• Advertisement
Sign in to follow this  

About enums in C++

This topic is 4148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Where do c++ programmers usally write thier ENUM types? lets say i got 5 enums, do i need to put each in diffrent header? Color.h , something1.h , something2.h ? enum Color { Red, Black }; enum something1 { ..., .... }; enum something2 { ..., .... }; enum something3 { ..., .... }; enum something4 { ..., .... };

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Thats a very vague question to which there isn't really an answer.
I would say it depends on scope and relativeness to other enum types.
So do the enum want to be global, in namespace, in a class?
Is there any tie betweeen the enums or are you just polluting the scope?

Share this post


Link to post
Share on other sites
You don't *need* to do it any particular way. If you really felt the need, you could put absolutely everything into a single source file. That would be a really bad idea, but you still could [smile]

Generally an enum doesn't make much sense on its own, it's usually associated with something. That something could be a set of flags to be passed to a function, for example. So the logical thing to do is to keep your enum in the same headers as that 'something' that it's associated with.

Share this post


Link to post
Share on other sites
yeah, there really isn't one specific way to arrange your enums. Logcally as joanusdmentia has said, an enum that is part of a class and/or function should logically be in the same header as that class/function. But of course not all enums are associated with one specific class/func. For example an enum that enumerates the keyboard keys - which is quite common should probably get it's own header file. Because this enum will be necessary in many different source files. But then again, maybe it's just part of your one window class and since all key presses are filtered in through the window class, you just need it in the header corresponding with your window class... So there's a valid case for both.

In short, do what you think feels right.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement