C++ enum redefenition and C++0x
If I want to exclude problems with enums and type redefinition in C++ I can use the code:
struct VertexType
{
enum
{开发者_JAVA百科
Vector2 = 1,
Vertor3 = 2,
Vector4 = 3,
};
};
struct Vector2 { ... };
struct Vector3 { ... };
struct Vector3 { ... };
Is there a way to remove the wrapper above enum. I looked at C++0x but didn't find addiditional inforamtion about solving this problem.
Since you are talking about C++0x, just use the new enum class
syntax:
enum class VertexType {
Vector1 = 1,
Vector2 = 2,
Vector4 = 3
};
The enumerator values will only be accessible through the VertexType
type as in VertexType::Vector1
.
Some quotes from the standard:
§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
}
How about namespace
?
namespace VertexType
{
enum V
{
Vector2 = 1,
Vertor3 = 2,
Vector4 = 3,
};
}
struct Vector2 { ... };
struct Vector3 { ... };
struct Vector4 { ... };
It appears vector3 is already being used. You can do what your trying to do, however, vector3 cannot be used.
enum //VertexType
{
Vector2 = 1,
//Vector3 = 2,
Vector4 = 3,
};
struct Vector2 { ... };
//struct Vector3 { };
struct Vector3 { ... };
This works for me, no errors at all.
This is a link i found. http://www.kixor.net/dev/vector3/
精彩评论