如果我想在 C++ 中排除枚举和类型重定义的问题,我可以使用代码:
struct VertexType
{
enum
{
Vector2 = 1,
Vertor3 = 2,
Vector4 = 3,
};
};
struct Vector2 { ... };
struct Vector3 { ... };
struct Vector3 { ... };
有没有办法删除枚举上面的包装器。我查看了 C++0x,但没有找到有关解决此问题的其他信息。
最佳答案
因为你在谈论 C++0x,只需使用新的 enum class
语法:
enum class VertexType {
Vector1 = 1,
Vector2 = 2,
Vector4 = 3
};
枚举值只能通过 VertexType
类型访问,如 VertexType::Vector1
。
标准中的一些引述:
§7.2/2 [...] The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators. [...]
§7.2/10 [...] Each scoped enumerator is declared in the scope of the enumeration.[...]
// example in §7.2/10
enum class altitude { high=’h’, low=’l’ };
void h() {
altitude a; // OK
a = high; // error: high not in scope
a = altitude::low; // OK
}
关于C++ 枚举重新定义和 C++0x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6992593/