c++ - 我可以在枚举中使用枚举吗?

标签 c++ enums namespaces

示例代码:

#ifndef SPELL_ENUMS_H
#define SPELL_ENUMS_H

namespace spellEnums {
        // Cantrips
    enum LEVEL_ZERO 
    {
        enum EVOCATION 
        {
            _DANCING_LIGHTS
        };
        enum CONJURATION 
        {
            _ACID_SPLASH
        };
    };

};

所以我可以做像 LEVEL_ZERO::EVOCATION::_DANCING_LIGHTS 这样的事情?

尽管将所有 300 多个 3.5e Dungeons and Dragon 的类型定义为紧凑、易于阅读和方便访问的替代建议将非常表示赞赏。 :D

或者我必须像这样做蹩脚的命名空间:

namespace LEVEL_ZERO {
        // Cantrips
        enum EVOCATION 
        {
            _DANCING_LIGHTS
        };
        enum CONJURATION 
        {
            _ACID_SPLASH
        };

};

namespace LEVEL_ONE {
        // Level one spells
        enum EVOCATION 
        {
            _FLAMING_HANDS
        };
        enum CONJURATION 
        {
            _MAGE_ARMOUR //BECAUSE JE SUIS CANADIEN le poutine eh?! 
        };

};

或者这会导致奇怪的问题吗?

最佳答案

我不认为嵌套枚举是一种好方法,我宁愿使用这样的方法:

enum _spell_enum 
    {
    _spell_evocation_beg=0x00000000,
    _spell_dancing_lights0,
    _spell_dancing_lights1,
    _spell_dancing_lights2,
    _spell_dark_shroud0,
    _spell_dark_shroud1,
    _spell_dark_shroud2,
    _spell_...,
    _spell_evocation_end,

    _spell_conjuration_beg=0x01000000,
    _spell_acid_splash0,
    _spell_acid_splash1,
    _spell_acid_splash2,
    _spell_acid_beam0,
    _spell_acid_beam1,
    _spell_acid_beam2,
    _spell_...,
    _spell_conjuration_end,

    _spell_teleport_beg=0x02000000,
    _spell_teleport_home,
    _spell_teleport_town_a,
    _spell_teleport_town_b,
    _spell_teleport_town_c,
    _spell_teleport_town_d,
    _spell_...,
    _spell_teleport_end,
    };

PS. 如果您需要额外的信息,例如等级,那么您可以使用包含所需信息的额外表格,或者使用 const int 而不是枚举并将信息直接编码到代码中(例如等级可以高或低 n 位)或者您可以按级别而不是拼写类型对枚举进行分组 ...

你的第二个解决方案也不好,因为我认为你需要唯一的法术 ID 并且单独的枚举是重叠的(除非你提供起始值)

关于c++ - 我可以在枚举中使用枚举吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19651243/

相关文章:

c++ - EncryptFile 函数是如何工作的,为什么我仍然可以保存内容?

c++ - 哪些因素应该决定我的 C++ 网络服务器上的线程数?

c++ - 如何在 WinApi (C++) 中创建计时器?

c++11 - 如何在另一个命名空间中使用一个命名空间的指针,并且两个命名空间都在另一个类中使用

.net - 在静态函数中获取命名空间

c++ - TOKEN_ELEVATION_TYPE 未在此范围内声明,我做错了什么?

java - 在 Spring MVC 应用程序中使用 Java 枚举?

c# - 为枚举值生成常量

namespaces - 如何在 C++/CLI 应用程序中正确设置根命名空间属性?

c++ - 我可以在类的构造函数中使用枚举吗?