Header inclusion question

J

Johannes Bauer

Hi group,

I've got a question concerning inclusion of .hpp files. Currently I'm
including all needed header files in the .cpp file. This means all
dependencies of the package and all dependencies of these dependencies
and so on.

This is quite ugly.

A start of an "Example.cpp" file could look like this

#include "ClassC.hpp" // Needed by ClassB
#include "ClassB.hpp" // Needed by Example
#include "Example.hpp"

Now what I could do would include the dependencies of dependencies in
the .hpp files and do a

#ifndef EXAMPLE_H
#define EXAMPLE_H
....
#endif

Yet I think this is quite ugly too...

Are there other alternatives?

Greetings,
Johannes

--
PLEASE verify my signature. Some forging troll is claiming to be me.
My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
Also: Messages from "Comcast Online" are ALWAYS forged.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBtigaCseFG8xyfi4RAj9uAJ9beapHtzBnDJSYslpeF++W78z1NgCfT8t8
YZQ5lusdrq/Gdp+/Bjxyck8=
=YuDA
-----END PGP SIGNATURE-----
 
E

E. Robert Tisdale

Johannes said:
I've got a question concerning inclusion of .hpp files.
Currently, I'm including all needed header files in the .cpp file.
This means all dependencies of the package
and all dependencies of these dependencies and so on.



This is quite ugly.

A start of an "Example.cpp" file could look like this

#include "ClassC.hpp" // Needed by ClassB
#include "ClassB.hpp" // Needed by Example
#include "Example.hpp"

Now what I could do would include the dependencies of dependencies in
the .hpp files and do a

#ifndef GUARD_EXAMPLE_H
#define GUARD_EXAMPLE_H 1
// contents of header file
#endif//GUARD_EXAMPLE_H

Yet I think this is quite ugly too...

Are there other alternatives?

Your header files appear to be module interfaces.
Each such header file should be both

1. idempotent and
2. self-sufficient.

Use "guard" macros as shown above
to ensure that the contents of your header file
is parsed only once by the C preprocessor.

Each header file should include *all*
od the header files that it requires.
This means that, in this case,
the only header file that you need to include
in Example.cpp is Example.hpp
 
A

Andre Kostur

Hi group,

I've got a question concerning inclusion of .hpp files. Currently I'm
including all needed header files in the .cpp file. This means all
dependencies of the package and all dependencies of these dependencies
and so on.

This is quite ugly.

A start of an "Example.cpp" file could look like this

#include "ClassC.hpp" // Needed by ClassB
#include "ClassB.hpp" // Needed by Example
#include "Example.hpp"

Now what I could do would include the dependencies of dependencies in
the .hpp files and do a

#ifndef EXAMPLE_H
#define EXAMPLE_H
...
#endif

Yet I think this is quite ugly too...

Are there other alternatives?

IMHO, each header file should be able to be compiled on its own. Each
header should also have its own include guards. So in your case, for
each of your header files I should be able to create a .cpp file in which
the only thing it does is #include that header file, that .cpp file
should compile.

So for your example I should be able to create an "Example.cpp" file with
one line in it:

#include "Example.hpp"

and I should be able to compile the Example.cpp file without any
problems. In your case that would mean

Example.cpp:

#include "Example.hpp"

Example.hpp:

#ifndef INC_EXAMPLE_HPP
#define INC_EXAMPLE_HPP

#include "ClassB.hpp"

void somefn(ClassB var);

#endif

ClassB.hpp:

#ifndef INC_CLASSB_HPP
#define INC_CLASSB_HPP

class ClassB
{
public:
void someotherfn(ClassA var);
};

#endif

ClassA.hpp:

#ifndef INC_CLASSA_HPP
#define INC_CLASSA_HPP

class ClassA
{
public:
void somethirdfn();
};

#endif
 
V

Victor Bazarov

Johannes said:
I've got a question concerning inclusion of .hpp files. Currently I'm
including all needed header files in the .cpp file. This means all
dependencies of the package and all dependencies of these dependencies
and so on.

This is quite ugly.

A start of an "Example.cpp" file could look like this

#include "ClassC.hpp" // Needed by ClassB
#include "ClassB.hpp" // Needed by Example
#include "Example.hpp"

Now what I could do would include the dependencies of dependencies in
the .hpp files and do a

#ifndef EXAMPLE_H
#define EXAMPLE_H
...
#endif

What does this have to do with dependencies?
Yet I think this is quite ugly too...

Are there other alternatives?

The Only Right Way is to include only the headers that are needed to
resolve symbols in _that_ module (whether it's a translation unit or
another header).

If your 'ClassB.hpp' needs 'ClassC.hpp', then 'ClassB.hpp' should include
it, not 'Example.cpp'. If your 'Example.hpp' needs 'ClassB.hpp', then it
itself should include 'ClassB.hpp'.

Now, it's a different story when we start talking _needing_ to include
anything. And you can reduce the includes by forward-declaring as much as
possible. For example, if your class definition only declares the use of
some class by a pointer or a reference, there is no need for the compiler
to know the other class' definition.

Victor
 
L

Lawrence Troxler

: IMHO, each header file should be able to be compiled on its own.

Yes.

: So for your example I should be able to create an "Example.cpp" file with
: one line in it:

: #include "Example.hpp"

: and I should be able to compile the Example.cpp file without any
: problems.

Why go through all this trouble? With good development tools, you should
be able to directly compile the header file.
There should be no need to create this "Example.cpp" just for testing purposes.

Of course, there are probably no "good development tools" in this context.
In a certain Microsoft Windows GUI C++ development environment, for
example, it doesn't work from the GUI - you can't simply open a header file
in the IDE editor and compile it.

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,208
Latest member
RandallLay

Latest Threads

Top