英文:
Why can't I create an int* pointer to an enum variable whose base type is int?
问题
我有这个:
enum GameObjectType : int
{
GameObjectType_STATIC = 0,
GameObjectType_LOOTABLE = 1,
GameObjectType_LANDMARK = 2,
GameObjectType_PORTAL = 3,
GameObjectType_QUEST = 4,
GameObjectType_LOOTABLE_BOX = 5,
};
GameObjectType mytype = GameObjectType_LANDMARK;
然而这是无效的:
int* myPtr = &mytype;
为什么无效,有没有办法拥有指向枚举的 int 指针?
英文:
I have this:
enum GameObjectType : int
{
GameObjectType_STATIC = 0,
GameObjectType_LOOTABLE = 1,
GameObjectType_LANDMARK = 2,
GameObjectType_PORTAL = 3,
GameObjectType_QUEST = 4,
GameObjectType_LOOTABLE_BOX = 5,
};
GameObjectType mytype = GameObjectType_LANDMARK;
However this is invalid:
int* myPtr = &mytype;
Why is that and is there any way to have int pointer to enum ?
答案1
得分: 1
因为 GameObjectType
和 int
是不同的类型。
没有(假设你希望指针可以被解引用)。
你可以定义一个“高级指针”类,内部指向这样的枚举,但其间接操作符返回一个 int
。
英文:
> Why is that
Because GameObjectType
and int
are different types.
> is there any way to have int pointer to enum ?
No (assuming you want the pointer to be dereferencible).
You could define a "fancy pointer" class that internally points to such enum, but whose indirection operator returns an int
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论