Here’s my problem:
I have 1 class that creates two new instances of a two other classes, and now I would need to either have a direct availability from one of the instances to the other, is this possible and if so, how?
so:
in Class A:
b = new B(5); // where 5 is an int data member of B
c = new C();
and let B::bee() be a function I would need to access from C() BUT, I would need to access members of B (not a new instance of B) created by A.
So C::getIntfromB(){
b->getInt();
}
Or something.
I’ve tried going for:
b = new B(5);
c = new C(*b);
having a B b; private member for C
However for some reason I simply cannot get it to work properly, what is the obvious error I am making?
Your
Cmember should be a pointer or a reference (B *borB &b). If it’s aBit will copy yourBobject, so it will refer to a different instance (which has been created by copying the one you’re interested into).Also, you’ll need to pass your
bobject as a pointer or a reference, otherwise a new instance will be created while passing the parameter, and your pointer/reference inCclass will refer to that one. (This would also lead to other problems, since such copy would be a temporary getting destroyed as soon asC‘s constructor returns)