Thread: An interesting simple code of forward declaration

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    An interesting simple code of forward declaration

    There are 2 classes, A and B:
    Code:
    ---------------------------------
    FileA.h
    ---------------------------------
    #include <FileB.h>
    class B; //Why forward declaration here
    class A{
    ...
    B objB;
    ...
    };
    ---------------------------------
    FileA.cc
    ---------------------------------
    #include <FileA.h>
    ...implementation of class A...
    
    
    
    
    ---------------------------------
    FileB.h
    ---------------------------------
    class A; //  I understand this is forward declaration 
    class B{
    ...
    A objA;
    ...
    };
    
    ---------------------------------
    FileB.cc
    ---------------------------------
    ...Implementation of class B
    What I don't understand is that why forward declare class B in FileA.h? Seems FileA.h already included FileB.h.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're right, it wouldn't technically be required to have that forward declaration, because including FileA.h would result in the following code:
    Code:
    class A; //  I understand this is forward declaration 
    class B{
    ...
    A objA;
    ...
    };
    
    class B; //Why forward declaration here
    class A{
    ...
    B objB;
    ...
    };
    However, spurious forward declarations never hurt anyone (that I've ever heard of). It might at least make it clearer to the reader that B is a class, without them having to delve into FileB.h to find out.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that your example is a little off, since the forward declaration of A in FileB.h is not enough to have a member variable of type A inside B.

    One possible reason for this in real code would be if the programmer was afraid of recursive includes. However, if a forward declaration is good enough with the #include, then the #include should be removed anyway, and if it is not, then it won't help in a situation of recursive includes, so I would argue it isn't a good idea anyway.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Hi, Daved. I don't quite understand you. Why "forward declaration of A in FileB.h is not enough to have a member variable of type A inside B." But the code can run correctly, can't it?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If all the compiler has of a class (or structure) is a forward declaration, you can't declare an instance of that class. The best you can do is to declare a pointer to it.

    Imagine what could happen otherwise.
    Code:
    class two;
    class one { two t; };
    class two { one o; };
    
    int main() {}
    Oops. Dinkumware's online compiler complains with
    Code:
    sourceFile.cpp(2) : error C2079: 'one::t' uses undefined class 'two'
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    dwks explained what I was referring to. In your example, class B has a member of type A, but if a cpp file includes FileB.h without including FileA.h before it, then the compiler won't have enough information about A to compile B properly.

    No matter how the includes are done, your example cannot compile anyway. The class B has a member of type A, so to know how big B is the compiler needs to know how big A is. But A has a member of type B, so to know how big A is the compiler needs to know how big B is. But B has a member of type A... and so on.

    If you have a pointer or reference to A in B (or B in A), then it is fine, because the compiler doesn't need to know the size of A, it just needs the size of a pointer or reference and a forward declaration will do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. forward declaration
    By C_ntua in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2008, 11:29 AM
  3. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 01:31 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Replies: 14
    Last Post: 11-23-2005, 08:53 AM