class declaration versus class header #include

A

Andrew

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
 
V

Victor Bazarov

Andrew said:
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
 
Y

yurec

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.
 
J

Juha Nieminen

Andrew said:
(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.
 
V

Victor Bazarov

Juha said:
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
 
J

Juha Nieminen

Victor said:
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.
 
V

Victor Bazarov

Juha said:
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
 
W

werasm

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
 
S

Sherman Pendley

Victor Bazarov said:
And the final result is?...


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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top