C++ 11中的静态导入(例如枚举类)

Jos*_* MN 5 c++ enums c++11 enum-class

我对枚举类(VS2012)的使用:

class matrix {
public:
    enum class operation_type {ADD, MULT};
    matrix(operation_type op);
...
}
Run Code Online (Sandbox Code Playgroud)

在我使用的另一个片段中

matrix* m = new matrix(matrix::operation_type::ADD);
Run Code Online (Sandbox Code Playgroud)

如果名字很长,这就变得非常混乱.

有可能以某种方式导入枚举值,以便我可以写:

matrix* m = new matrix(ADD);
Run Code Online (Sandbox Code Playgroud)

嵌套类也一样 - 我可以导入它们吗?

And*_*owl 5

,这是不可能的.

你不能省略该operation_type部分,因为你已经使它成为一个范围的枚举(这就是范围枚举的全部内容).如果你想避免它,你必须使它成为无范围enum(删除class关键字).

此外,matrix您不能通过using声明导入成员名称,就像matrix命名空间一样.此外,根据C++ 11标准的第7.3.3/7段:

using声明不应命名范围的枚举器.