CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2009
    Posts
    68

    Self-referencing header files

    Hello,

    I have two classes, Class1 and Class2, declared in two header files, "class1.h" and "class2.h". Each class contains a member instance of the other class. If I #include the necessary header files as standard, then I get a compiler error because it creates an unlimited referencing loop. How can I solve this?

    Thanks!

    Code:
    //"class1.h"
    
    #include "class2.h"
    
    class Class1
    {
        Class2 a_class2;
    };
    
    
    //"class2.h"
    
    #include "class1.h"
    
    class Class2
    {
        Class1 a_class1;
    };

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Self-referencing header files

    You can't actually put an instance of each class inside the other. Think about it for a second:

    You have two cardboard boxes. Put one inside the other. Clearly, the outer box must be slightly bigger than the inner one to fit it.

    Now, put the outer box inside the inner box too----without removing it from the outer box. Clearly this is insane and won't work in normal Euclidean space.

    Similarly, you can't do what you are trying to do because you are creating a situation in which sizeof(Class1) > sizeof(Class2) and simultaneously sizeof(Class2) > sizeof(Class1). It's a contradiction.

    Now, what you *can* do is put a pointer to a Class1 object inside of a Class2 object and vice versa. This is because the size of a pointer is constant and doesn't depend the size of the type it points to. Furthermore, you can do this without knowing the full definition of the pointed-to class, which is critical, because it means you can use forward declaration to solve the cyclical include dependency:

    Code:
    //"class1.h"
    
    class Class2;
    
    class Class1
    {
        Class2 *a_class2;
    };
    
    
    //"class2.h"
    
    class Class1
    
    class Class2
    {
        Class1 *a_class1;
    };
    Note, however, that in the corresponding source files you'll need to include the actual headers, eg, class1.cpp will need to #include "class2.h" and vice versa. However this does not pose a problem.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Self-referencing header files

    Don't include the header, just use a forward declaration of the class.

    Code:
    //"class1.h"
    
    class Class2;
    
    class Class1
    {
        Class2 a_class2;
    };
    
    
    //"class2.h"
    
    class Class1;
    
    class Class2
    {
        Class1 a_class1;
    };
    then #include the header in the implementation file.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Self-referencing header files

    Ack! missed the fact that it's not class pointers.

    What Lindley said!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured