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

class declaration versus class header #include

P: n/a
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
Share this Question
Share on Google+
9 Replies


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

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

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

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

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

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

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

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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.