How to initialize non constants in a header?


How can i give dynamic assignments in header files.

eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//(--in the header file for all "globals"--)
//prototype and definition of myname for all to see.
class _myclass;
_myclass * myname;


//(--this in a sepatate header file, same namespace--)
//my class is in separate file to keep main header file cleaner looking.
class _myclass()
{
blah
blah
blah
};

myname = new _myclass();


I want to organise my headers in such a way to keep my "_internal" stuff out of the way in a separate file while keeping an easy to read set of definitions in my "globals" for other people to see what they are called (with commented help)

Last edited on
It's not a good idea to define or initialize global variables in headers. If two .cpps that are linked together include a header that defines a global variable, you'll get duplicate definition linker error.
If you need to use globals (which is also something you should avoid) you should do it like this:
1
2
3
4
5
6
7
//file.h
extern T global_var;
extern T2 global_obj;

//file.cpp
T global_var=something;
T2 global_obj(some_other_thing);
If you want to see what 'professional' header files look like, just look at
the header files that come with your compiler.

If you want a challenge- try following through the STL implementation headers
such as for vectors, lists, etc..
Topic archived. No new replies allowed.