Can't resolve complexly mutually dependent classes

Hello, can't quite see where to go with this.

I can't change A because it's part of a library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class A {
  public:
    void foo() {[...]}
};
class C;
class B {
  public:
    C *deltaC;
    B(C *gammaC) {
      deltaC = gammaC; //(is there a better way of doing this?)
    }
    void bar() {
      deltaC->alphaA->foo();  // <-- ** problem :( **
    }
};
class C {
  public:
    friend class B;
    A *alphaA;
    B *betaB;
    baz() {
      betaB = new B(this);
      betaB->bar();
    }
};

The forward declaration isn't sufficient for B::bar() because the pointer to the object of class C is dereferenced, but C's declaration cannot be made before B's, because C::baz() creates a B object. I can't see how I can make a sort of "interface" abstract class for C because of B::bar()'s use of A.

Is there any way around this problem?
Don't inline B::bar.

Or if you do, do it after C is defined and not inside of B.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class A
{
//...
};
class C;
class B
{
//...
  void bar();
};

class C
{
//...
};

inline void B::bar()
{
  deltaC->alphaA->foo();
}



Of course, you can simplify this and make it less fugly by putting A, B, and C in their own headers, and using the "right way" to include headers as described here:

http://www.cplusplus.com/forum/articles/10627/#msg49679

Specifically, read sections 4, 6, 7.
Ah, thank you. I'm an idiot.
I suppose another way would be this:
1
2
3
4
5
6
7
8
class Cforward {
  public:
    A a;
}
class B { [...] }
class C : public class Cforward {
[...] //[don't declare a again]
}

but defining B::bar later makes more sense.
Last edited on
Topic archived. No new replies allowed.