B
Brian
Hi all,
I have two objects, A, which has-a B. At the same time, B is related
enough to A, that it needs to call methods from A... I can only think
of how to do this with the following method. It seems like there must
be a better way, because I dont really like what I am doing... that
is, setting the B object's owner to A's this on construction. Does
anyone have any suggestions?
class A;
class B {
public:
int get_down_tonight() { return owner->do_a_little_dance(); }
void set_owner(A *a) { owner = a; }
private:
A *owner;
};
class A {
public:
int do_a_little_dance() {return j; };
A() {b.set_owner(this);
private:
B b;
int j;
};
main(void)
{
A myA;
int i;
i = A.b.get_down_tonight(); //call do_a_little_dance() from myA
}
I have two objects, A, which has-a B. At the same time, B is related
enough to A, that it needs to call methods from A... I can only think
of how to do this with the following method. It seems like there must
be a better way, because I dont really like what I am doing... that
is, setting the B object's owner to A's this on construction. Does
anyone have any suggestions?
class A;
class B {
public:
int get_down_tonight() { return owner->do_a_little_dance(); }
void set_owner(A *a) { owner = a; }
private:
A *owner;
};
class A {
public:
int do_a_little_dance() {return j; };
A() {b.set_owner(this);
private:
B b;
int j;
};
main(void)
{
A myA;
int i;
i = A.b.get_down_tonight(); //call do_a_little_dance() from myA
}