Hi Ignazio,
Personally, I try to put everything in the .H file. The reason is it makes
the generated code in-line (performance) and all in one place (source
organization). With so much RAM in computers nowadays it usually makes sense
to optimize for speed rather than size, and in-line usually contributes in
this regard.
However, there are times you MUST put stuff in a .CPP file. This is when you
have two classes which both must reference each other in a 'detailed' way
(i.e., each calls a method in the other). If just the pointer is needed to
an instance of another class, one can still stay in the .H by declaring the
other class before it is used without defining it.
That is, this is ok to stay in two different .H files:
--------------------------------
----------------------------------
header file A:
#include "B.h"
class A
{
void Method_A()
{
m_B_Ptr->Method_B() ;
} ;
:
B* m_B_Ptr ;
} ;
--------------------------------
header file B:
class A;
class B
{
:
A* m_A_Ptr ;
} ;
-------------------------------
-------------------------------
But this is NOT ok:
-------------------------------
-------------------------------
header file A:
#include "B.h"
class A
{
void Method_A()
{
m_B_Ptr->Method_B() ;
} ;
:
B* m_B_Ptr ;
} ;
-----------------------------
header file B:
class A; // replacing this with #include "A.h" causes circular definition
errors
class B
{
void Method_B()
{
m_A_Ptr->Method_A( ) ; // error since doesn't know about Method_A
}
:
A* m_A_Ptr ;
} ;
-----------------------------
-----------------------------
This can be corrected by changing the B.H file and adding a B.CPP like so:
-----------------------------
-----------------------------
header file B:
#include "A.h" ; // change declaration to included A.h
class B
{
void Method_B() ; // declaration only
:
A* m_A_Ptr ;
} ;
------------------------------
CPP file B:
#include "B.h" ;
ClassB::Method_B()
{
m_A_Ptr->Method_A( ) ; // no error
}
-----------------------------
-----------------------------
Note that B.CPP gets its definition of class A from the include of A.h in
B.h.
Also, it is STRONGLY recommended you place a "#pragma once" at the top of
each .H file (but never in a .CPP file) which means if it occurs more than
once in a definition the compiler will ignore all but one include of this
header file. That way you can have more than one .H file included in the
same definition, all of which have a common .H included (e.g., program
equates/defines header), file without problems...
[==P==]