forwarddeclaration, declaration, definition, order still a matter?

Discussion in 'C++' started by Henning Hasemann, Jun 14, 2006.

  1. 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
     
    Henning Hasemann, Jun 14, 2006
    #1
    1. Advertising

  2. Henning Hasemann

    rolkA Guest

    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
     
    rolkA, Jun 14, 2006
    #2
    1. Advertising

  3. Henning Hasemann

    mlimber Guest

    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
     
    mlimber, Jun 14, 2006
    #3
  4. Wow, im really fascinated about your speed :)
    Thanks rolkA and mlimber for the enlightenment!

    Henning
     
    Henning Hasemann, Jun 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Does domain still matter?

    John, Jun 29, 2003, in forum: Java
    Replies:
    2
    Views:
    881
    Shripathi Kamath
    Jun 29, 2003
  2. SpaceCowboy
    Replies:
    6
    Views:
    539
    SpaceCowboy
    Aug 14, 2003
  3. hilz
    Replies:
    1
    Views:
    570
    Richard Tobin
    Jan 18, 2006
  4. Replies:
    3
    Views:
    464
    James Kanze
    Jun 12, 2008
  5. Anders Wegge Keller

    Conditional declaration (A matter of style)

    Anders Wegge Keller, Oct 13, 2012, in forum: C Programming
    Replies:
    3
    Views:
    385
    Kaz Kylheku
    Oct 13, 2012
Loading...

Share This Page