Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 24-Feb-13 23:22pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
...
  Permalink  
v3
Comments
Matra Divat at 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 at 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)
Matra Divat at 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 at 25-Feb-13 5:40am
   
No it's not, but more neat than having it global.
Stefan_Lang at 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.
Stefan_Lang at 25-Feb-13 6:48am
   
Note that even in VisualStudo you can choose a different name for your precompiled header than stdafx.h. In fact, doing so may be useful when combining multiple projects within a single solution: it can be confusing when you have two or more of these open in an editor window and you don't know which is which...

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

  Print Answers RSS
0 OriginalGriff 335
1 Peter Leow 135
2 Zoltán Zörgő 130
3 Afzaal Ahmad Zeeshan 115
4 DamithSL 100
0 Sergey Alexandrovich Kryukov 7,442
1 OriginalGriff 5,996
2 Peter Leow 4,292
3 CHill60 2,858
4 Mika Wendelius 2,850


Advertise | Privacy | Mobile
Web04 | 2.8.150224.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100