By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

include .h files in .h

P: n/a
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
Apr 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rares Vernica wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 5 '06 #2

P: n/a
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.

Apr 5 '06 #3

P: n/a
>
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/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Interaction Modeling
Tool

Apr 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.