Recursive imports problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Recursive imports problem

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    6

    Recursive imports problem

    I have a problem with recursive imports in C++. I have this 2 classes:

    //File A.h
    #include "B.h"

    class A{
    public:
    A(){};

    void doA(B b){ b.doB(); }
    }

    //File B.h
    #include "A.h"

    class B{
    public:
    B(){};

    void doB(A a){ a.doA(); }
    }

    I tried to use forward declarations but it doesn't work because i call methods "doA" and "doB" so I cant use forward declarations.

    Do someone know what can I do to compile it?

    Thank you!

  2. #2
    Join Date
    May 2007
    Posts
    13

    Re: Recursive imports problem

    You can use forward declarations and hide the object implementation behind a pointer.

    So A.h would look like

    Code:
    class B;
    class A{
    public:
      A(){}
    
      void doA(B *b);
    };
    A.cpp would be:

    Code:
    #include "A.h"
    #include "B.h"
    
    void A::doA(B *b){
      b->doB( this );
    }
    B.h would be:

    Code:
    class A;
    class B{
    public:
      B(){};
    
      void doB(A* a);
    };
    and B.cpp would be:

    Code:
    #include "B.h"
    #include "A.h"
    
    void B::doB(A *a){
      a->doA( this );
    }
    Regards.

  3. #3
    Join Date
    Jan 2010
    Posts
    6

    Re: Recursive imports problem

    Thank you very much, it works!!!

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center