Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old April 5th, 2006, 01:05 AM
Rares Vernica
Guest
 
Posts: n/a
Default include .h files in .h

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
  #2  
Old April 5th, 2006, 01:15 AM
Phlip
Guest
 
Posts: n/a
Default Re: include .h files in .h

Rares Vernica wrote:
[color=blue]
> Is it a good practice to include other .h files in a .h file?[/color]

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".
[color=blue]
> 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.[/color]

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!!!
  #3  
Old April 5th, 2006, 01:15 AM
guyarad@gmail.com
Guest
 
Posts: n/a
Default Re: include .h files in .h

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.

  #4  
Old April 5th, 2006, 04:15 AM
EventHelix.com
Guest
 
Posts: n/a
Default Re: include .h files in .h

>[color=blue]
> 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.
>[/color]
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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.