473,383 Members | 1,737 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,383 software developers and data experts.

class declaration versus class header #include

Hi,

I see some people prefer to use in headers (where possible) class
declaration:

// B.h
class A;

class B
{
A* m_pa;
};

instead of including the class header file.

// B.h
#include "A.h"

class B
{
A* m_pa;
};
is it just a personal preference or is it more (like faster compile
time) ?

thank you
Dec 4 '07 #1
9 1726
Andrew wrote:
I see some people prefer to use in headers (where possible) class
declaration:

// B.h
class A;

class B
{
A* m_pa;
};

instead of including the class header file.

// B.h
#include "A.h"

class B
{
A* m_pa;
};
is it just a personal preference or is it more (like faster compile
time) ?
The latter. If the compiler doesn't *need* to see the definition,
there is no sence for it to see it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '07 #2
On Dec 4, 3:51 pm, Andrew <nites...@gmail.comwrote:
Hi,

I see some people prefer to use in headers (where possible) class
declaration:

// B.h
class A;

class B
{
A* m_pa;

};

instead of including the class header file.

// B.h
#include "A.h"

class B
{
A* m_pa;

};

is it just a personal preference or is it more (like faster compile
time) ?

thank you
There is also nice chapter in Satter Exceptional c++, Compiler
Firewalls and the Pimpl Idiom.
Maybe you find it interesting.
Dec 4 '07 #3
yurec wrote:
[..]
There is also nice chapter in Satter Exceptional c++, Compiler
<nitSutter's </nit>
Firewalls and the Pimpl Idiom.
Maybe you find it interesting.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '07 #4
Andrew wrote:
(like faster compile time) ?
IMO it's not really the faster compile time (after all, it's quite
fast for the compiler to parse two header files, certainly not
significantly slower than parsing just one) as much as it's decreasing
dependencies between source files. The (sometimes very significantly)
faster compile times come indirectly from this. When a header file is
changed, the less dependencies to it there are in the program, the less
compilation units need to be recompiled.

Nothing is more irritating and time-consuming than having to make a
small change in a header file and then having to wait 15 minutes for the
program to recompile. By decreasing header dependencies it may well be
that that recompilation time is reduced to 30 seconds or whatever along
those lines.
Dec 4 '07 #5
Juha Nieminen wrote:
Andrew wrote:
>(like faster compile time) ?

IMO it's not really the faster compile time (after all, it's quite
fast for the compiler to parse two header files, certainly not
significantly slower than parsing just one) as much as it's decreasing
dependencies between source files. The (sometimes very significantly)
faster compile times come indirectly from this. When a header file is
changed, the less dependencies to it there are in the program, the
less compilation units need to be recompiled.
And the final result is?...
>
Nothing is more irritating and time-consuming than having to make a
small change in a header file and then having to wait 15 minutes for
the program to recompile. By decreasing header dependencies it may
well be that that recompilation time is reduced to 30 seconds or
whatever along those lines.
So, it's not really the faster compile time, right? Then what is it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '07 #6
Victor Bazarov wrote:
So, it's not really the faster compile time, right? Then what is it?
Compiling from scratch doesn't become significantly faster, but in
certain situations making a change and then recompiling can become much
faster.
Dec 7 '07 #7
Juha Nieminen wrote:
Victor Bazarov wrote:
>So, it's not really the faster compile time, right? Then what is it?

Compiling from scratch doesn't become significantly faster, but in
certain situations making a change and then recompiling can become
much faster.
So, "re-compiling" is not really "compiling" then, is it? Faster
*re*-compiling cannot be called "faster compiling". I get it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 7 '07 #8
On Dec 4, 2:51 pm, Andrew <nites...@gmail.comwrote:
is it just a personal preference or is it more (like faster compile
time) ?
Apart from all the other valid comments, I could perhaps
add that often, when having a name dependency to an
interface in another namespace, or perhaps a template,
depending on the likelihood of the class/interface changing,
I have contemplated simply including instead of a forward
declaration. This is even more the case when the association
is provided from externally (not via factory), implying
that the using class would have to include as well regardless.

I'm not particularly fond of :

namespace X{ namespace Y{
template <class T, class U>
class SomeClass;
}
}

Regards,

Werner
Dec 7 '07 #9
"Victor Bazarov" <v.********@comAcast.netwrites:
Juha Nieminen wrote:
>Andrew wrote:
>>(like faster compile time) ?

IMO it's not really the faster compile time (after all, it's quite
fast for the compiler to parse two header files, certainly not
significantly slower than parsing just one) as much as it's decreasing
dependencies between source files. The (sometimes very significantly)
faster compile times come indirectly from this. When a header file is
changed, the less dependencies to it there are in the program, the
less compilation units need to be recompiled.

And the final result is?...
>>
Nothing is more irritating and time-consuming than having to make a
small change in a header file and then having to wait 15 minutes for
the program to recompile. By decreasing header dependencies it may
well be that that recompilation time is reduced to 30 seconds or
whatever along those lines.

So, it's not really the faster compile time, right? Then what is it?
Faster build time. The compile time for each individual file is unchanged,
but reducing dependencies usually means fewer files need to be recompiled
for a given change, reducing the overall build time.

sherm--

--
WV News, Blogging, and Discussion: http://wv-www.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Dec 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Eisenberg | last post by:
Hi! I have a header with two classes, WidthLogger and Hyst. WidthLogger is declared first; its ctor takes a Hyst reference. Hyst declares WidthLogger a friend. Hyst constructs a WidthLogger...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
8
by: Sim Smith | last post by:
This is the problem: I have to inherit a third party class file in my library. If I directly do it, I will have to ship the third party header files to my customers as well. I do not want to...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
9
by: silversurfer2025 | last post by:
Hello everyone, I am currently having problems with a C++ abstract class. I have a class FrameWork.h which defines some methods (of which some are abstract, i.e. virtual void method() = 0). In...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
19
by: bubzilla | last post by:
Hi, i´ve got about 10 headerfiles with implemented classes. Now when i try to compile them i get the following message: In file included from Proxy/ServerCnx.hh:36, from...
17
by: Amchi | last post by:
Alright .... this makes no sense ... Declared a class 'diskStorage' in a header ...diskStorage.h Defined it's contructor and methods ... in diskStorage.cpp Included diskStorage header in...
4
by: zfareed | last post by:
#include <iostream> #include <fstream> using namespace std; template<class ItemType> class SortedList { private:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.