Connecting Tech Pros Worldwide Forums | Help | Site Map

class interaction

Dan
Guest
 
Posts: n/a
#1: Jul 22 '05
How can I let two classes call a method from each other,
like in this example?


class A {
B* myB;
void doSomething() {
myB->doSomething();
}
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};

Thanks,
Dan

Attila Feher
Guest
 
Posts: n/a
#2: Jul 22 '05

re: class interaction


Dan wrote:[color=blue]
> How can I let two classes call a method from each other,
> like in this example?
>
>
> class A {
> B* myB;
> void doSomething() {
> myB->doSomething();
> }
> };
>
> class B {
> A* myA;
> void doSomething() {
> myA->doSomething();
> }
> };
>
> Thanks,
> Dan[/color]

class B;

class A {
B* myB;
inline void doSomething();
};

class B {
A* myA;
void doSomething() {
myA->doSomething();
}
};


inline void A::doSomething() {
myB->doSomething();
}

And you perfect endless loop is ready, but it will not be endless (eats up
the call-stack).

--
Attila aka WW


Sumit Rajan
Guest
 
Posts: n/a
#3: Jul 22 '05

re: class interaction



"Dan" <dansteinhauer@hotmail.com> wrote in message
news:7d784848.0402040336.58f59c62@posting.google.c om...[color=blue]
> How can I let two classes call a method from each other,
> like in this example?
>
>
> class A {
> B* myB;
> void doSomething() {
> myB->doSomething();
> }
> };
>
> class B {
> A* myA;
> void doSomething() {
> myA->doSomething();
> }
> };
>
> Thanks,
> Dan[/color]



class B;

class A {
B* myB;
public:
void doSomething(); //I'm assuming you want this to be public
};

class B {
A* myA;
public:
void doSomething() {myA->doSomething();}
};

void A::doSomething()
{
myB->doSomething();
}

Regards,
Sumit.


Bob Smith
Guest
 
Posts: n/a
#4: Jul 22 '05

re: class interaction


Dan wrote:
[color=blue]
> How can I let two classes call a method from each other,
> like in this example?
>
>
> class A {
> B* myB;
> void doSomething() {
> myB->doSomething();
> }
> };
>
> class B {
> A* myA;
> void doSomething() {
> myA->doSomething();
> }
> };
>
> Thanks,
> Dan
>[/color]

class A{
B * m_b;
public:
A( B * b ):m_b( b ){};
void b_do(){
m_b->doSomething();
}
void doSomething(){};
};

class B{
A * m_a;
public:
B( A * a ):m_a( a ){};
a_do(){
m_a->doSomething();
}
void doSomething(){};
}




Martijn Lievaart
Guest
 
Posts: n/a
#5: Jul 22 '05

re: class interaction


On Wed, 04 Feb 2004 03:36:04 -0800, Dan wrote:
[color=blue]
> How can I let two classes call a method from each other,
> like in this example?
>[/color]

class B;[color=blue]
>
> class A {
> B* myB;
> void doSomething() {
> myB->doSomething();
> }[/color]

void doSomething();
[color=blue]
> };
>
> class B {
> A* myA;
> void doSomething() {
> myA->doSomething();
> }
> };
>[/color]

void A::doSomething()
{
myB->doSomething();
}

Note the order. Class B is forward declared, therefore class A can use a
pointer to B. It cannot (yet) use B itself, therefore we moved the
implementation of A::doSomething down.

HTH,
M4

Default User
Guest
 
Posts: n/a
#6: Jul 22 '05

re: class interaction


Dan wrote:[color=blue]
>
> How can I let two classes call a method from each other,
> like in this example?[/color]


http://www.parashift.com/c++-faq-lit...html#faq-38.11



Brian Rodenborn
Claudio Puviani
Guest
 
Posts: n/a
#7: Jul 22 '05

re: class interaction


"Dan" <dansteinhauer@hotmail.com> wrote[color=blue]
> How can I let two classes call a method from each other,[/color]

A number of replies were already given on how to do it. Let me add this: unless
the classes are intended to be tightly coupled and jointly represent a single
interface, as with containers and their iterators, you should NOT introduce
circular dependencies in your code. That makes the code difficult to test,
maintain, and read. If you ever encounter an APPARENT need to have circular
dependencies, reconsider the problem in light of a third class that acts as a
coordinator between the two. That way, you have one class that depends on two
independent classes.

Claudio Puviani


Closed Thread