Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > forwarddeclaration, declaration, definition, order still a matter?

Reply
Thread Tools

forwarddeclaration, declaration, definition, order still a matter?

 
 
Henning Hasemann
Guest
Posts: n/a
 
      06-14-2006
Hi all,
On larger projects I used the following 'technique' (well in fact its a
really simple thing):

1. Write one source-code file for each 'large' class and one
corresponding header file. (code in source-code file declaration in
header file, as usual)

2. Write a central header file which first holds forward declarations of
all classes an then includes all other header files.

3. Include the central header fillfrom all source-code files, so the
declarations of every class is available everywhere.

I think thats a usual practice (correct me, if Im wrong).
I always though, this way I whould not have to care about teh ordering
of the header-file inclusion, but when a class uses members of whose
types are other self-defined classes the forward declaration does not
seem to be enough.

Short example:

// main.h

class A;
class B;
#include "a.h"
#include "b.h"

// a.h
class A {
private:
B mSomething; // Forward declaration of B not enough here
// ...
};

The same problem arises when I try to inherit from a class which is just
forward-declared.

Can I fix this without finding out an unproblematic order of header
files and without making pointers out of every Member?

Whats the usual approach to this problem?

TIA
Henning


 
Reply With Quote
 
 
 
 
rolkA
Guest
Posts: n/a
 
      06-14-2006
Hi,
The forward declaration is not enough since the compiler need to know
the value of 'sizeof(A)', so it needs to know 'sizeof(B)', and that's
why a forward declaration is not enough: The class must be defined, not
just declared.

That's not a problem. Just include "b.h" in "a.h". That's the common
practice.



Henning Hasemann wrote:
> Hi all,
> On larger projects I used the following 'technique' (well in fact its a
> really simple thing):
>
> 1. Write one source-code file for each 'large' class and one
> corresponding header file. (code in source-code file declaration in
> header file, as usual)
>
> 2. Write a central header file which first holds forward declarations of
> all classes an then includes all other header files.
>
> 3. Include the central header fillfrom all source-code files, so the
> declarations of every class is available everywhere.
>
> I think thats a usual practice (correct me, if Im wrong).
> I always though, this way I whould not have to care about teh ordering
> of the header-file inclusion, but when a class uses members of whose
> types are other self-defined classes the forward declaration does not
> seem to be enough.
>
> Short example:
>
> // main.h
>
> class A;
> class B;
> #include "a.h"
> #include "b.h"
>
> // a.h
> class A {
> private:
> B mSomething; // Forward declaration of B not enough here
> // ...
> };
>
> The same problem arises when I try to inherit from a class which is just
> forward-declared.
>
> Can I fix this without finding out an unproblematic order of header
> files and without making pointers out of every Member?
>
> Whats the usual approach to this problem?
>
> TIA
> Henning


 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      06-14-2006
Henning Hasemann wrote:
> Hi all,
> On larger projects I used the following 'technique' (well in fact its a
> really simple thing):
>
> 1. Write one source-code file for each 'large' class and one
> corresponding header file. (code in source-code file declaration in
> header file, as usual)


Good.

> 2. Write a central header file which first holds forward declarations of
> all classes an then includes all other header files.


What?! Do you think you're Bill Gates or something?

> 3. Include the central header fillfrom all source-code files, so the
> declarations of every class is available everywhere.


No, no, no. See http://www.gotw.ca/publications/mill04.htm.

> I think thats a usual practice (correct me, if Im wrong).


It's a common practice, but that doesn't mean it's a good practice. On
a 'larger projects' this practice can significantly and unnecessarily
slow build times.

> I always though, this way I whould not have to care about teh ordering
> of the header-file inclusion, but when a class uses members of whose
> types are other self-defined classes the forward declaration does not
> seem to be enough.
>
> Short example:
>
> // main.h
>
> class A;
> class B;
> #include "a.h"
> #include "b.h"
>
> // a.h
> class A {
> private:
> B mSomething; // Forward declaration of B not enough here
> // ...
> };
>
> The same problem arises when I try to inherit from a class which is just
> forward-declared.
>
> Can I fix this without finding out an unproblematic order of header
> files and without making pointers out of every Member?
>
> Whats the usual approach to this problem?


As the article above states, you should only include necessary files to
reduce dependencies. It's tempting to use short cuts, but for 'larger
projects' this is usually a mistake. Here you should include only b.h
in a.h.

Cheers! --M

 
Reply With Quote
 
Henning Hasemann
Guest
Posts: n/a
 
      06-14-2006
Wow, im really fascinated about your speed
Thanks rolkA and mlimber for the enlightenment!

Henning
 
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
Events / ViewState Order etc Still Confused mosscliffe ASP .Net 0 06-20-2006 10:57 AM
If you get an order # does it mean the order is accepted? =?Utf-8?B?U3RldmUxMDc3?= Windows 64bit 3 05-12-2005 11:46 PM
Traversion order cf. output order in XSL Soren Kuula XML 2 02-01-2004 09:10 AM
In which order are files looked for when loaded/requierd - and what'sthe order of suffixes? Stephan Kämper Ruby 2 01-18-2004 02:07 PM
How to Display DropDownList with preserved order (custom order) cspoh ASP .Net Web Controls 0 07-31-2003 09:19 AM



Advertisments