Connecting Tech Pros Worldwide Help | Site Map

class interaction

  #1  
Old July 22nd, 2005, 07:07 AM
Dan
Guest
 
Posts: n/a
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
  #2  
Old July 22nd, 2005, 07:07 AM
Attila Feher
Guest
 
Posts: n/a

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


  #3  
Old July 22nd, 2005, 07:07 AM
Sumit Rajan
Guest
 
Posts: n/a

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.


  #4  
Old July 22nd, 2005, 07:07 AM
Bob Smith
Guest
 
Posts: n/a

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(){};
}




  #5  
Old July 22nd, 2005, 07:07 AM
Martijn Lievaart
Guest
 
Posts: n/a

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

  #6  
Old July 22nd, 2005, 07:08 AM
Default User
Guest
 
Posts: n/a

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
  #7  
Old July 22nd, 2005, 07:08 AM
Claudio Puviani
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
CodeDom - Namespace/Class interaction between Ns1 and CodeDom's Ns2 thisismykindabyte answers 0 October 22nd, 2008 01:22 AM
Class interaction question Angus answers 3 August 25th, 2007 10:35 AM
OOP: True hierarchical parent/child class interaction? Namespaces? Paul answers 7 November 21st, 2005 09:21 PM