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

    [RESOLVED] Class heirarchy help (classes using eachother)

    Heya!

    I have Class A that uses a variable of Class B and Class B uses a variable of Class A

    I'm having issues on how to include them in such a way that I don't get errors. I tried using

    Code:
    #ifndef class_a
    #define
    
     ...
    
    #endif
    And I did the same for Class B and tried to include them in each other's file but that didn't solve the problem.

    Does anyone know a fix for this?

    Thanks!
    Lang
    Last edited by Lang; February 2nd, 2009 at 03:32 PM. Reason: Resolved.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Class heirarchy help (classes using eachother)

    What you are looking for is called 'forward referencing'.

    Code:
    // this tells the compiler that there is a class 'a', but 
    // we are not telling the compiler what it looks like
    // the compiler will figure that out later...
    class a;
    
    class b;
    {
    public:
       a *some_a_var;
    
    };

  3. #3
    Join Date
    Jan 2009
    Posts
    16

    Re: Class heirarchy help (classes using eachother)

    Cheers!

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Class heirarchy help (classes using eachother)

    easy you just need to use a forward declaration to stop the circular dependency:

    http://en.wikipedia.org/wiki/Circular_dependency
    ahoodin
    To keep the plot moving, that's why.

Tags for this Thread

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