user53670
user53670

Reputation:

Where to put the enum in a cpp program?

I have a program that uses enum types.

enum Type{a,b,};


class A
{
 //use Type
};
class B
{
  // also use that Type
};

2 class are located in 2 different files. Should I put the type definition in a headfile or in class definition for each class?

Upvotes: 34

Views: 41705

Answers (5)

sly
sly

Reputation: 1780

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).

Upvotes: 4

Dolphin
Dolphin

Reputation: 4762

It really depends on if the values are the same logical type, or if they just happen to have the same names. Would it make sense to assign an A::Type variable to a C::Type? If they are the same logical type, put them in a header that both include. To keep your build times low you probably want to put it in its own header file, but putting it in a shared header with other stuff works if you want to keep the number of files down.

Another option is to put the enum in a common base class that both inherit from (this may not make sense in this case, but it is another option).

Upvotes: 0

anon
anon

Reputation:

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
   ...
};

Upvotes: 8

avakar
avakar

Reputation: 32635

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.

Upvotes: 1

Nathan Fellman
Nathan Fellman

Reputation: 127428

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

Upvotes: 48

Related Questions