J
Jon Rea
I am currently cleaning up an application which was origainlly hashed
together with speed of coding in mind and therefore contains quite a few
"hacky" shortcuts.
As part of this "revamping" process I am introducing namespaces to
properly compartmentalise sections of the code into logical units. What
I am speciffically trying to get right is the dependency tree for header
files to reduce compile time and simplify the code structure.
On this subject I have a few "best practise" questions:
1) I realise that the whole point of a .h file is to keep the interface
separate from the implementation. What I would like to understand better
is exaclty what one should normally place in header files and if there
are exceptions.
More specifically where should you place includes of system headers and
system namespaces? Should these normally go in the .h or the .cpp file?
Sometimes (i.e. when you are using templates) you need to put
implementation code in the header file. This implplementation is then
included in everything that references that header. If the file is used
lots, and/or many files cross-reference each other then the compilation
process is going to become slower and more complicated. Therefore header
files should reference each other as little as possible????
Are there any good internet articles on this subject (and on the other
things in this post)?
2) What is best practise when classes are interdependent and in
different .h files? (Is adding 'class Class2;' the best way?):
In class1.h:
----------------------
#include "class2.h"
class Class2; // How does it work internally in the compiler? I read
somewhere that there is there any runtime penatly for this, but I cant
see why this would be so. Is there any penalty?
class Class1
{
private:
Class2 m_C2;
};
----------------------
In class2.h:
----------------------
#include "class1.h"
class Class1;
class Class2
{
private:
Class1 m_C2;
};
----------------------
3) Where should "using namespace" directievs go, in the .cpp file or in
the .h file?
Obviously compilation times are increased as interdependency inceases
between header files. Header files should be kept to a bare minimum in
what they include. Does adding a "using namsespace" to a header cause
extra compilation? or is its just effectively a set of default
namespaces in which to look for class names?
We have these classes:
in file ClassNS1.h
----------------------
namespace NS1
{
class Class1
{
};
}
----------------------
in file ClassNS2.h
----------------------
namespace NS2
{
class Class1
{
};
}
----------------------
Is there any fundamental difference between these in terms of the
compilation process:
in file invoke.h
----------------------
#include "ClassNS1.h"
using namespace NS1; // does this compile all of NS1 and include it
(i.e. big compile time penalty) ? or is this just a syntax shortcut
(i.e. no compile time penalty)?
class Invokation
{
public:
void Go();
private:
Class1 *m_Class1;
};
----------------------
in file invoke.cpp
#include "invoke.h"
void Invokation::Go()
{
m_Class1 = new Class1();
}
in main.cpp
----------------------
void main()
{
Invokation *iv = new Invokation();
iv->Go();
}
----------------------
OR THIS ??:
in file invoke.h
----------------------
#include "ClassNS1.h"
class Invokation
{
public:
void Go();
private:
NS1::Class1 *m_Class1;
};
in file invoke.cpp
#include "invoke.h"
void Invokation::Go()
{
m_Class1 = new Class1();
}
----------------------
in main.cpp
----------------------
void main()
{
Invokation *iv = new Invokation();
iv->Go();
}
together with speed of coding in mind and therefore contains quite a few
"hacky" shortcuts.
As part of this "revamping" process I am introducing namespaces to
properly compartmentalise sections of the code into logical units. What
I am speciffically trying to get right is the dependency tree for header
files to reduce compile time and simplify the code structure.
On this subject I have a few "best practise" questions:
1) I realise that the whole point of a .h file is to keep the interface
separate from the implementation. What I would like to understand better
is exaclty what one should normally place in header files and if there
are exceptions.
More specifically where should you place includes of system headers and
system namespaces? Should these normally go in the .h or the .cpp file?
Sometimes (i.e. when you are using templates) you need to put
implementation code in the header file. This implplementation is then
included in everything that references that header. If the file is used
lots, and/or many files cross-reference each other then the compilation
process is going to become slower and more complicated. Therefore header
files should reference each other as little as possible????
Are there any good internet articles on this subject (and on the other
things in this post)?
2) What is best practise when classes are interdependent and in
different .h files? (Is adding 'class Class2;' the best way?):
In class1.h:
----------------------
#include "class2.h"
class Class2; // How does it work internally in the compiler? I read
somewhere that there is there any runtime penatly for this, but I cant
see why this would be so. Is there any penalty?
class Class1
{
private:
Class2 m_C2;
};
----------------------
In class2.h:
----------------------
#include "class1.h"
class Class1;
class Class2
{
private:
Class1 m_C2;
};
----------------------
3) Where should "using namespace" directievs go, in the .cpp file or in
the .h file?
Obviously compilation times are increased as interdependency inceases
between header files. Header files should be kept to a bare minimum in
what they include. Does adding a "using namsespace" to a header cause
extra compilation? or is its just effectively a set of default
namespaces in which to look for class names?
We have these classes:
in file ClassNS1.h
----------------------
namespace NS1
{
class Class1
{
};
}
----------------------
in file ClassNS2.h
----------------------
namespace NS2
{
class Class1
{
};
}
----------------------
Is there any fundamental difference between these in terms of the
compilation process:
in file invoke.h
----------------------
#include "ClassNS1.h"
using namespace NS1; // does this compile all of NS1 and include it
(i.e. big compile time penalty) ? or is this just a syntax shortcut
(i.e. no compile time penalty)?
class Invokation
{
public:
void Go();
private:
Class1 *m_Class1;
};
----------------------
in file invoke.cpp
#include "invoke.h"
void Invokation::Go()
{
m_Class1 = new Class1();
}
in main.cpp
----------------------
void main()
{
Invokation *iv = new Invokation();
iv->Go();
}
----------------------
OR THIS ??:
in file invoke.h
----------------------
#include "ClassNS1.h"
class Invokation
{
public:
void Go();
private:
NS1::Class1 *m_Class1;
};
in file invoke.cpp
#include "invoke.h"
void Invokation::Go()
{
m_Class1 = new Class1();
}
----------------------
in main.cpp
----------------------
void main()
{
Invokation *iv = new Invokation();
iv->Go();
}