Problem with cyclic dependency

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soup007
    New Member
    • Apr 2008
    • 3

    Problem with cyclic dependency

    Hi,
    I am having some difficulties with cyclic dependency between two classes. Situation is something like following -

    Code:
    ///A.h
    #include "B.h"
    
    class A {
    {
       int X;
    public:
       P() { Q(); }
       Q() {
          R();
          bb = new B();
          bb.D( this ); )
       R() { uses X )
    
    };
    
    ///B.h
    class B: public someclass {
    public:
       D(A& a) {
          a.Q();        //end condition for recursion depends on X 
          a.R();
    
    };
    SOME BACKGROUND-
    I couldn't create D() in A as D() access private data in B. But D() calls
    both Q() and R() and value of X has to be same everywhere. i.e. R()
    called from D access the same X value that was used by class A. That's why I am passing this pointer of class A to B. There will be just one instance of A and B. But there isn't just one class B, there are lots of classes like B derived from a parent class. I am actually creating objects for these classes, kind of linked list. A has functions for creating a list, but it may be nested that's why there is a recursion.

    PROBLEM-
    Forward declaration of A in B doesn't work and I can't include header of A to B as header of B is included in A. I am using kdevelop on suse.
    How do I solve this thing? Sorry for such a long description.
    Any help would be very appreciated. Thanks.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    You cannot use forward declaration because you need to access class methods, and by giving it only a forward declaration there is no way for compiler to know which are the methods and members of the forward declared class.
    Instead try declaring a interface for class A in B.h which will contain Q() and R(). That interface will be passed as reference to D in class B.In A.h you will derive class A from this interface and trough polymorphism you should be able to get this to work.

    Savage

    Comment

    • soup007
      New Member
      • Apr 2008
      • 3

      #3
      Originally posted by Savage
      You cannot use forward declaration because you need to access class methods, and by giving it only a forward declaration there is no way for compiler to know which are the methods and members of the forward declared class.
      Instead try declaring a interface for class A in B.h which will contain Q() and R(). That interface will be passed as reference to D in class B.In A.h you will derive class A from this interface and trough polymorphism you should be able to get this to work.

      Savage
      Thanks a lot for the reply, I will just try that.

      Comment

      • soup007
        New Member
        • Apr 2008
        • 3

        #4
        Beautiful, works like a charm.
        Thanks

        Comment

        Working...