forwarddeclaration, declaration, definition, order still a matter?

H

Henning Hasemann

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
 
R

rolkA

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

mlimber

Henning said:
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
 
H

Henning Hasemann

Wow, im really fascinated about your speed :)
Thanks rolkA and mlimber for the enlightenment!

Henning
 

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

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,302
Latest member
endevsols

Latest Threads

Top