使用来自另一个类的枚举的 C++

c++ using enum from another class

本文关键字:枚举 C++ 另一个      更新时间:2023-10-16

我创建了一个类Adresy

class Adresy {
    public:
        static const DWORD hp = 0x947000;
        static const DWORD mp = 0x7B2084;
        static const DWORD cap = 0x97EE94;
        enum Flags
        {
            None = 0,
            Poisoned = 1,
            Burning = 2,
            ProtectedByMagicShield = 16
        };
};

当我尝试在此示例中使用它时:

if(( (DWORD) adr.ProtectedByMagicShield & pFlags) == (DWORD) ProtectedByMagicShield){
//...
}

它说抛出错误:'ProtectedByMagicShield' : undeclared identifier...

pFlags是一个DWORD,我使用的是C++.NET。

if(( (DWORD) Adresy::ProtectedByMagicShield & pFlags) == (DWORD) Adresy::ProtectedByMagicShield){
    //...
}

需要使用类名和范围标记 (::)以访问枚举的值。

这是因为枚举不是由类的任何特定实例拥有,而是由类本身拥有,例如静态 const 成员。