Reputation: 187
I'm having some problems with forward declaration, well, I'm confused about everything. I have 3 classes that depend on each other. For example:
I want to know how can I use forward declarations in such a manner to avoid circular dependencies.
Thanks for your help.
Upvotes: 0
Views: 81
Reputation: 1285
#include <iostream>
using namespace std;
class B;
class C;
class A{
public:
static B instanceOfB;
static C instanceOfC;
static void foo(){
cout << "A static foo called"<< endl;
}
};
class C{
static A instanceOfA;
};
A C::instanceOfA = A();
class B{
A instanceOfA;
};
B A::instanceOfB = B();
int main(){
cout << "main run"<<endl;
A::foo();
return 0;
}
1st, it is a bad design when class depends on each other. 2nd, this kind of depends should doesn't mean you have to hold the object of the class, instead, if this dependent is complicated, you need to use a "pointer" to the object instead of holding the object. "Pointer to object" means compiler will not require the full prototype of that class.
Upvotes: 1