I have three classes, class A, class B, and class C.

Each class is in its own file.
Class A uses class B. (object class)
Class B uses class A. (object class)
Class C uses class A and class B. (program class)

How do I go about using #include and/or prototypes so that this chain works?

Recommended Answers

All 5 Replies

> Class A uses class B. (object class)
> Class B uses class A. (object class)
One of these will have to contain only a POINTER to the other one.

class A {
  B b;
};
class B {
  A a;
};

You just end up with a.b.a.b.a.b.a.b.a.b.a.b.a.b and so on filling up the universe.

Making one of them a pointer to the other breaks the cycle.

class B; // class forward declaration
class A {
  B *b;
};
class B {
  A a;
};

Wouldn't you just end up with this then?
a.b->a.b->a.b->a.b to the end of the universe?

No, because a->b requires you to actually run some code along the lines of

a->b = new B;
or something.

It's like a linked list, which points to itself.
Whereas this is an alternating pair of types.

No, because a->b requires you to actually run some code along the lines of

a->b = new B;
or something.

It's like a linked list, which points to itself.
Whereas this is an alternating pair of types.

Ahh yes that makes sense. Not too sure why you would need to do something like this, could you post the context of your problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.