In C++, is there some way to create a class that have, as attribute, an object of a class that have, as attribute, an object of the first class?
e.g.:
class A {
B attribute;
public:
A(){}
};
class B {
A attribute;
public:
B(){}
};
The code above does not compile. Is there some way to do something alike?
Thanks!
First, you need to forward declare your class B. If not, the compiler wouldn’t know what the B is. Also, change the attributes to be pointers, or else you will still have a compiler error, even though you forward declared B, it still doesn’t know the implementation yet!
The following code may help, good luck…