473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Apr 5 '06 #1
3 2150
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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chad Scharf | last post by:
I have a legacy ASP application running on IIS 6.0 (Windows Server 2003 Web Edition) that is throwing an error when processesing a certain asp page that has about 200 or so include directives. ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
7
by: mescaline | last post by:
Hi, Suppose a_file.cpp contains a function a_function() Now to include it in main_file.cpp I just do #include "a_file.cpp" and I'm all set. i recently came across this seemingly roundabout...
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
1
by: ya man | last post by:
when i use #include <iostream.h> in some files i get lots of error messages of the kind 'ambiguous symbol this is solved when i use #include <iostream why is that ? and can i use #include...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
2
by: key9 | last post by:
Hi all look at the organize tree main.c ------ #include lib_adapter.c main() { foo();
16
by: Chris Shearer Cooper | last post by:
In our Visual Studio 2005 application, we have several of our application's H files that are #included into stdafx.h. What is odd, is that when we change those application H files, VS2005...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.