include .h files in .h

R

Rares Vernica

Hi,

Is it a good practice to include other .h files in a .h file?

For example, if I have a .h file that uses other two .h files I have two
options:
1. include the two used .h files in the .h file that uses them; in the
main .cpp file only include the top .h file
2. do not include .h files in any .h file; in the main .cpp file include
all the .h files

I wonder how bad is the first option.

I am currently using the second option and I end-up including 10 .h
files and just 1 of them is related to my .cpp file, the rest of 9 are
dependencies.

Is there any other option?

Thanks,
Ray
 
P

Phlip

Rares said:
Is it a good practice to include other .h files in a .h file?

Yes and no. Here's the best tip, from the book /Large Scale C++ Software
Design/ by John Lakos:

The first line of Foo.cpp shall always be #include "Foo.h".

That means the compiler always gets a chance to compile Foo.h once, alone,
without any other headers above it.

If Foo.h uses std::string, it should #include <string>.

And of course you should use "include guards".
I am currently using the second option and I end-up including 10 .h
files and just 1 of them is related to my .cpp file, the rest of 9 are
dependencies.

Now here's the No side of the answer.

If class Foo uses a huge class SimCity, it should only use it like this:

class SimCity; // forward declare
class Foo
{
public:
Foo(SimCity const & aCity); // a "bald reference"
};

Foo doesn't need to #include "SimCity.h", because _how_ it uses that city
encapsulates inside Foo.cpp. Other modules do need to know the name of
SimCity, so they can compile the interface to Foo correctly, but many of
them don't need to know what a SimCity object really looks like.

These techniques make C++ scalable, without endless recompiles after we
change header files, even important ones.
 
G

guyarad

Here is what I think...
Basically, a class should be independent.
Whan I want to use your class, i.e. including your header files, I
don't want to be forced to include any other headers!
I think, that if you need any header files in your class, you should
include them yourself.

Although, if the additional header files you need are related only to
you PRIVATE section of your class, it is better to avoid including them
in the header file of the class.
How do you do that? there is a mechanism called forward declaration.
It is used to tell the compiler "somewhere in my code, I have declared
class XXX, so trust me, it is there".
How the compiler can use this? Doesn't the compiler have to have all
declarations to compiler one's header? well, not always.
The only case it doesn't have to, is when you use a pointer to certain
class.

For example:
You want to use class Depend in your private section of class Major:
class Major {
Depend m_myDepend;
public:
....
....
};
In that case you have to include Depend.h in your Major.h.
But, if you use a pointer to Depend:
Depend *m_pMyDepend
you can instead of including Depend.h add the following line _before_
your class declaration:
class Depend;
That's it!
Important:
1. Of course, in the cpp file of class Major you have to include
Depend.h if you want to use and instanciate that class.
2. If you have a return value of one of the public methods you export
from Major, you SHOULD include Depends.h in Major.h, because when I use
your class I want to be able to use that method without having to
include any more headers.
 
E

EventHelix.com

Is it a good practice to include other .h files in a .h file?

For example, if I have a .h file that uses other two .h files I have two
options:
1. include the two used .h files in the .h file that uses them; in the
main .cpp file only include the top .h file
2. do not include .h files in any .h file; in the main .cpp file include
all the .h files

I wonder how bad is the first option.

I am currently using the second option and I end-up including 10 .h
files and just 1 of them is related to my .cpp file, the rest of 9 are
dependencies.
Try replacing includes with forward declarations.

The following article should help:
http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top