How can i inside enum in class
Hellow, how can i inside enum in class?
I wanna that i can called enum item throught class:
- ClassName::EnumElement
If i declare enum outside class, for example
- enum En{One, Second};
- class c{
- }
I could write En::One instead of с::One, but if i write
- class c{
- enum En{One, Second};
- }
Compiler get me many errors
Advance many thanks for your help!
Edit: Moved to C++ Gurus as this is not Qt specific [ZapB]
8 replies
Just for the records: enums in C++ do not use the enum name for accessing enum values.
- class TestClass
- {
- public:
- enum Enum
- {
- Value1,
- Value2,
- Value3
- };
- };
- ...
- TestClass::Enum enumVariable = TestClass::Enum::Value1; // _not_ correct
- TestClass::Enum enumVariable = TestClass::Value1; // correct
- ...
This is why good class design usually requires that the enum name is part of the enum values (as also seen in Qt).
- class TestClass
- {
- public:
- enum Error
- {
- InternalError,
- ExternalError
- };
- };
And keep in mind that class members – as already stated by others – are private by default.
Just for the records: enums in C++ do not use the enum name for accessing enum values. (…) This is why good class design usually requires that the enum name is part of the enum values (as also seen in Qt).
Also note that this is about to change [en.wikipedia.org] in C++0x. There, the enum name does become part of the value name. You could change your second example to this then:
- class TestClass
- {
- public:
- class enum Error //note the 'class' in front of the enum keyword
- {
- Internal, //Note that you no longer need the Error postfix
- External
- };
- };
- ...
- TestClass::Error errorVariable = TestClass::Error::Internal; // correct in C++0x
You must log in to post a reply. Not a member yet? Register here!



