class interaction 
July 22nd, 2005, 06:07 AM
| | | class interaction
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 | 
July 22nd, 2005, 06:07 AM
| | | 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 | 
July 22nd, 2005, 06:07 AM
| | | 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. | 
July 22nd, 2005, 06:07 AM
| | | 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(){};
} | 
July 22nd, 2005, 06:07 AM
| | | 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 | 
July 22nd, 2005, 06:08 AM
| | | 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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|