Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > class declaration versus class header #include

Reply
Thread Tools

class declaration versus class header #include

 
 
Andrew
Guest
Posts: n/a
 
      12-04-2007
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2007
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


 
Reply With Quote
 
 
 
 
yurec
Guest
Posts: n/a
 
      12-04-2007
On Dec 4, 3:51 pm, Andrew <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2007
yurec wrote:
> [..]
> There is also nice chapter in Satter Exceptional c++, Compiler


<nit> Sutter'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


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-04-2007
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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-04-2007
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


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      12-07-2007
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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
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


 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      12-07-2007
On Dec 4, 2:51 pm, Andrew <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Sherman Pendley
Guest
Posts: n/a
 
      12-07-2007
"Victor Bazarov" <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM



Advertisments