In C# this is perfectly legal :
enum A
{
None,
Value1,
Value2
}
enum B
{
None,
Sometype1,
Sometype2
}
C++ complain that None is already define, so is it possible to accomplish the same thing in C++ ?
Thanks
-
-
No. According to the C standard, identifiers in an enumerator list must be distinct from all other identifiers declared in the same scope.
EDIT: After reading the C++ standard, it should seem that I'm wrong
If you declare your enum as enum class, you should be able to reuse names. So it would look like this:enum class A { None, Value1, Value2 } enum class B { None, Sometype1, Sometype2 } //... where you're using these enums B someVariable = B::None; B someOtherVariable = B::Value1; A yetAnotherVariable = A::None;
Downsides: you have to use A:: or B:: to access the enum's values and you can't treat your enumeration's values as an int or bool anymore (so int number = A::None and A enumValue = 1 will not work).
EDIT2: enum class doesn't seem to be in the C++ 1998 standard... your mileage may vary (it may or may not be implemented in Visual Studio; can't try right now). Guess I should be more careful which document I pull up (I grabbed the Oct. 2008 draft of the C++ standard). If it's not in VS, your best bet is to prefix your constants with some distinct prefix (like ANone and BNone).
-
enum class is a construct of C++/CLI, it defines a managed enum type. It is not available in regular C++.CannotResolveSymbol said:No. According to the C standard, identifiers in an enumerator list must be distinct from all other identifiers declared in the same scope.
EDIT: After reading the C++ standard, it should seem that I'm wrong
If you declare your enum as enum class, you should be able to reuse names. So it would
look like this:enum class A { None, Value1, Value2 } enum class B { None, Sometype1, Sometype2 } //... where you're using these enums B someVariable = B::None; B someOtherVariable = B::Value1; A yetAnotherVariable = A::None;
Downsides: you have to use A:: or B:: to access the enum's values and you can't treat your enumeration's values as an int or bool anymore (so int number = A::None and A enumValue = 1 will not work).
EDIT2: enum class doesn't seem to be in the C++ 1998 standard... your mileage may vary (it may or may not be implemented in Visual Studio; can't try right now). Guess I should be more careful which document I pull up (I grabbed the Oct. 2008 draft of the C++ standard). If it's not in VS, your best bet is to prefix your constants with some distinct prefix (like ANone and BNone).
I don't believe there is a way to do what you want using standard C++. At least not in C++98 or C99. -
enum class is planned for C++ 0x; when I pulled up the C++ standard, I grabbed the latest draft instead of the current version... my mistake.Sven Groot said:
enum class is a construct of C++/CLI, it defines a managed enum type. It is not available in regular C++.CannotResolveSymbol said:*snip*
I don't believe there is a way to do what you want using standard C++. At least not in C++98 or C99.
-
I see, I haven't been keeping up with the developments in C++0x. It's good to know though. Right now there's no way though.CannotResolveSymbol said:
enum class is planned for C++ 0x; when I pulled up the C++ standard, I grabbed the latest draft instead of the current version... my mistake.Sven Groot said:*snip*
VC9 already supports it, but as part as C++/CLI so it only works when compiling with /clr, unfortunately. Hopefully VC10 will support it in regular C++ too, which it might since they announced it'd have some C++0x features iirc. -
Maybe I can go with something like this until C++ 0x is release and implemented in gcc and VC compiler ?
class A
{
public:
enum Value { None, Value1, Value2 };
};class B
{
public:
enum Value { None, SomeType1, SomeType2 };
};int main()
{
A::Value a = A::None;
B::Value b = B::None;
} -
cro said:Maybe I can go with something like this until C++ 0x is release and implemented in gcc and VC compiler ?
class A
{
public:
enum Value { None, Value1, Value2 };
};class B
{
public:
enum Value { None, SomeType1, SomeType2 };
};int main()
{
A::Value a = A::None;
B::Value b = B::None;
}That'd work. With some trickery you would be able to declare variables without using the ::Value bit too, like this:
class MyEnum { public: enum Value { None, Value1, Value2 }; MyEnum(Value value) : _value(value) { } operator Value() const { return _value; } private: Value _value; };
Then you could simply use it as:MyEnum x = MyEnum::Value1;
You'd have to repeat that code for every enum though. -
Sven Groot said:cro said:*snip*
That'd work. With some trickery you would be able to declare variables without using the ::Value bit too, like this:
class MyEnum { public: enum Value { None, Value1, Value2 }; MyEnum(Value value) : _value(value) { } operator Value() const { return _value; } private: Value _value; };
Then you could simply use it as:MyEnum x = MyEnum::Value1;
You'd have to repeat that code for every enum though.Thanks !
-
OR, you could try using namespaces:
namespace A { enum AA{ None, Value1, Value2 }; } namespace B { enum BB { None, Sometype1, Sometype2 }; } ... ... int a = A::None; int aa = A::AA::None; int b = B::None; int bb = B::BB::None;EDIT: added some namespace/name permutations. PS: I don't think I like this new editor...
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.