Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I've heard it is possible to indicate in a C++ project
a header file which you want to be included throughout the
whole project (i.e., all classes can use it). where
can I put such an include? Thanks.

ps. If I declare an enum in one class, how can I use it in a different?
Posted

1 solution

I think you are reffering to the concept of precompiled headers. You have one single header file (in mfc it's stdafx.h) where you add the includes (mostly the ones that are often needed) and afterwards you are just include this header file in all other files. To be on the safe side, you can configure in your project settings, that this file is included automatically. A big advantage of precompile headers is the compilation of your project is much faster because files that haven't changed since last compile won't be compiled.

As for the enums, as long as you declare it public you can access it by class Scope:

class EnumWrapper
{
public:
  enum Colors
  {
     Red,
     Blue
  }
}


...
if(EnumWrapper::Red) doStuff();
...
 
Share this answer
 
v3
Comments
[no name] 25-Feb-13 4:39am    
hm... thanks about includes. about enums I was thinking they could be used like this.
For example we have class A:

class A
{
public:
enum Color{Red, Blue}
}

Then in class B, we could include class A header:

#include "a.h"

and I thought in B I could directly use:

Colors red;

isn't it?? and maybe also if (smth == Red) ???
max_nowak 25-Feb-13 5:04am    
According to what you want to accomplish, I'd suggest you to put the enum outside of 'Class A' for global use. Something like this:
namespace A
{
enum Color{Red, Blue}
}

This way you can take advantage of the keyword 'using'.
#include "a.h"
using namespace A

class B
{
void doStuff()
{
Color red;
if(Color==red)
... blablalba
}
}

Edit:
if you want to stick to your class, the farest you can go is doing this:

class A
{
public:
enum Color{Red, Blue}
}

#include "a.h"

typedef A::Color Color;

Color c;
if(c==A::Red)
[no name] 25-Feb-13 5:37am    
thanks for your comment. I believe the alternative would be also to just put enums in some header file (no class) -- and then I think I can use them directly right?? ps. is the namespace necessary, what if I just put enums outside the class declaration??
max_nowak 25-Feb-13 5:40am    
No it's not, but more neat than having it global.
Stefan_Lang 25-Feb-13 6:44am    
The reason for using a namespace is avoiding naming conflicts. In a project with just 5-10 files, this is not a problem, but once you have a team of developers working on dozens or hundreds of files, your names will inevitably clash.

What's worse is that the name clash may not be obvious right away: if some module uses a constant called "Red" all over the place, but doesn't actually use the Color enum, and thus doesn't include your header, it will compile and run just fine. But if at some point you decide you need that enum and include "a.h", then suddenly you get compiler errors all over the place and may need to fix a lot of code that by itself is totally ok! It is not the code that's wrong, it is the enum declaration that breaks it!

Using a namespace lets you clarify which "Red" you mean.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900