Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums: Can they do in .h or must stay in .cpp?

Tags:

c++

header

If I have something like:

enum {     kCP_AboutBox_IconViewID = 1,     kCP_AboutBox_AppNameViewID = 2,     kCP_AboutBox_VersionViewID = 3,     kCP_AboutBox_DescriptionViewID = 4,     kCP_AboutBox_CopyrightViewID = 5 }; 

in my .cpp can it go in the .h?

More so, what other lesser know things can you put in a .h besides class definitions, variables, etc, etc

like image 929
user147502 Avatar asked Aug 16 '09 15:08

user147502


People also ask

Should enums go in header file?

In general an enum is going to be used as a type definition and should always be in the header file.

Where do you keep enums?

Put the enums in the namespace where they most logically belong. (And if it's appropriate, yes, nest them in a class.)

How do enums work CPP?

Enum is a user-defined data type that consists of a fixed set of constants or we can say a set of integral constants. The enum keyword is used to define an enumeration in the C++ programming language. It can be used to represent a set of directions and days as enum are implicitly final and static.

Can enums be extended C++?

No, there is not. enum are really the poor thing in C++, and that's unfortunate of course. Even the class enum introduced in C++0x does not address this extensibility issue (though they do some things for type safety at least).


1 Answers

A .h file is essentially just code which, at compile time, is placed above any .cpp (or .h file for that matter) that it's included in. Therefore you CAN just place any code from the .cpp file into the .h and it should compile fine.

However it's the design which is important. Your code (e.g. your enum) SHOULD be placed in the .h file if you need to expose it to the code you're including the .h file. However if the enum is only specific to the code in your header's .cpp implementation, then you should encapsulate it just within the .cpp file.

like image 161
RemoteCTO Avatar answered Sep 21 '22 18:09

RemoteCTO