Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the enum in a cpp program?

Tags:

c++

People also ask

Where should enum be placed?

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

How do we use enum in C++?

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.

Should enum be in header or CPP?

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 .

Why enum is used in CPP?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.


If the enum is going to be used in more than one .cpp file, you should put it in a header file that will be included by each. If there's a common header file, you should use that, otherwise you may as well create a new header file for this enum


You should always attempt to limit the scope of types in C++, so the enum should probably be declaread at class scope. The enum will typically belong slightly more naturally in one class than the other - lets say class A, so you put it in the declaration of A in the a.h header:

// a.h

class A {
    public:
       enum Type { a, b };
    ...
};

Now you need to include a.h in the header that declares B:

// b.h
#include "a.h"

class B {
   public:
      void f( A::Type t );     // use the Type enum
   ...
};

I can see the point of Neil: it is a pet peeve for many programmers to see stuff on the global scope. otoh, imho, introducing a class just for an enum is not a good style: It is supposed to be enum not a class. However, putting the same enum list in both classes (is what you were asking) would be the worst idea: we don't want to be repeating stuff.

Moreover, in most non-trivial codes, one might end up using more of such shared entities (more enums, const parameters, etc...) for implementation. So, I'd begin lumping all this into an implementation namespace (say "detail") which is a child namespace of your classes, and resides in a separate header file (say "detail.hpp"), included by all. For example:

// file A.hpp 
#include "foo/detail.hpp"
namespace foo {
   class A 
   { 
   // accessing enum as detail::a 
   };
}

// file B.hpp
#include "foo/detail.hpp"
namespace foo { class B { ... }; }  

// file foo/detail.hpp
namespace foo { namespace detail {
   enum { a,b, ... }
   const int three = 3;
   // etc...
   // other implementation classes etc...
}}

And "detail" is nice and clean way of warning your class users to back off from whatever's declared in there. As your code gets bigger and these implementation details start growing in number you can break the dependencies into separate header files (detail1 detail2 etc...) and still keep one "detail" namespace (something which you can not do with a "class detail" for example).


The question is rather vague, but as a rule of thumb, you should try to minimize the redundancy in your code. Therefore, you should put the declaration of the enum to a header file.