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

forwarddeclaration, declaration, definition, order still a matter?

P: n/a
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
Jun 14 '06 #1
Share this Question
Share on Google+
3 Replies


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


Jun 14 '06 #2

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

Jun 14 '06 #3

P: n/a
Wow, im really fascinated about your speed :-)
Thanks rolkA and mlimber for the enlightenment!

Henning
Jun 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.