【问题标题】:Abbreviated syntax for a C++11 enum class nested in a class?嵌套在类中的 C++11 枚举类的缩写语法?
【发布时间】:2015-10-04 14:33:03
【问题描述】:

我使用了一个 C++11 库,它的头文件带有一个嵌套在类中的枚举类:

class ClassWithARatherLongName
{
public:
    enum class EnumWithARatherLongName
    {
        VALUE1,
        VALUE2,
        VALUE3
    };
    (other code)
};

然后我在我的代码中使用枚举器:

void foo()
{
    ... ClassWithARatherLongName::EnumWithARatherLongName::VALUE1 ...
}

这可行,但很乏味。没有办法缩写语法吗?

理想情况下,我希望能够编写如下内容:

void foo()
{
    (some directive that allows to use an abbreviated syntax)
    ... VALUE1 ...
}

【问题讨论】:

    标签: c++11 enums nested


    【解决方案1】:

    您可以使用typedefusing 创建“快捷方式”:

    using E = ClassWithARatherLongName::EnumWithARatherLongName;
    E::VALUE1
    

    【讨论】:

    • 几乎和我希望的一样短。如果有办法摆脱E::,那就更好了。
    【解决方案2】:

    只需添加一个 typedef:

    void foo()
    {
        typedef ClassWithARatherLongName::EnumWithARatherLongName e;
        e::VALUE1
    }
    

    您可以通过使用命名空间来缩短名称。

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2016-03-31
      • 2013-03-13
      • 1970-01-01
      • 2012-10-09
      相关资源
      最近更新 更多