为什么我不能创建一个指向基类型为int的枚举变量的int*指针?

huangapple go评论73阅读模式
英文:

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

因为 GameObjectTypeint 是不同的类型。

没有(假设你希望指针可以被解引用)。

你可以定义一个“高级指针”类,内部指向这样的枚举,但其间接操作符返回一个 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.

huangapple
  • 本文由 发表于 2023年5月18日 06:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定