PDA

View Full Version : How to write an enum in order QtCreator gives me 'auto' when use ::



tonnot
28th April 2011, 18:14
I have a class with a public enum declared at .h. for example myenum.
Into implementation I want to have a way to 'view' myenum elements.
Need I to define I namespace ? Use myenum:: gives me nothing.
Any idea? (maybe it is a very basic question....)
Thanks

aamer4yu
28th April 2011, 18:52
If you use some good IDE, I guess you can see those values through auto completion.

SwissKnife
28th April 2011, 21:49
With the following class definition, using the class name MyClass:: lists the enumerators eZero, eOne, eTwo. ( use ctrl-SPACE to start auto completion)


class MyClass {
public:
enum eMyEnum { eZero, eOne, eTwo };
}

tonnot
29th April 2011, 00:19
Thanks.
The problem is that ctr space offers me a lot of function and members. I'd want to see only the enum values I want.
If I know that I want values for a specific enum it must be exists a way to do it.... but I dont know how to.

DanH
29th April 2011, 01:21
I think you want too much.

Enums have never fit smoothly into C/C++ (or Java, or most other languages). The enum name in C/C++ is pretty meaningless -- for the most part just syntactic sugar. Some versions of Pascal did it a bit differently, but it still was awkward.

Probably about the best you can do is embed the enum in its own class (possibly as an inner class in languages that allow it). Or "smarten" your IDE somehow so that it will know that Ctrl-E means "show me the next enum".

ChrisW67
29th April 2011, 02:07
The declaration of an enum in C++ does not create a new naming scope as, for example, declaring a new class or struct does. There is no MyEnum::Value. The enum values become members of the scope that contains the enum declaration and that is what every code completion tool I have seen shows (and certainly what every C++ compiler does when resolving these names). So, for example, you cannot compile this:


struct Test {
enum OneType { One, Two, Three };
enum AnotherType { Three, Four, Five };
OneType one;
AnotherType another;
};



$ g++ -c main.cpp
main.cpp:3: error: declaration of ‘Three’
main.cpp:2: error: conflicts with previous declaration ‘Test::OneType Test::Three’


You can wrap your enums in classes to also gain type safety, but a typical approach to this using templates, also breaks some code completion implementations (e.g. Qt Creator).
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Safe_Enum

Berryblue031
29th April 2011, 08:58
In general I use the following naming convention for enums:



enum STATUS { STATUS_GOOD, STATUS_BAD };

This provides the following perks:
- when the autocomplete pops up for my class I know what is an enum because its all caps
- when the autocomplete pops up the values of the enum are grouped together because of the prefix
- name clashes are also prevented because of the prefix

tonnot
29th April 2011, 09:08
Thanks to everybody.
I already are using a naming convention as BerryBlue said.