473,237 Members | 1,120 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,237 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 1721
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, ive 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:
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.