c++ - Define object from other classes -
i can't define object other classes under top class example :
class b; class { public: a(){ cout << "a\n"; } ~a(){ cout << "~a\n"; } private: b ob; }; class b { public: b(){ cout << "b\n"; } ~b(){ cout << "~b\n"; } ob; }; int main() { b r; system("pause"); }
the problem can't make object in class if write (class b;) above it
your problem solved passing b
object pointer a
constructor. if change b
class contain pointer a
, recursion solved. is, creating infinite loop, creating object b
contains object a
, contains object b
, , on.
class b { public: b(a* _ob) : ob(_ob) { cout << "b\n"; } ~b(){ cout << "~b\n"; } a* ob; }; int main() { _a; b r(_a); system("pause"); }
Comments
Post a Comment