Connecting Tech Pros Worldwide Help | Site Map

class interaction

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 06:07 AM
Dan
Guest
 
Posts: n/a
Default 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

  #2  
Old July 22nd, 2005, 06:07 AM
Attila Feher
Guest
 
Posts: n/a
Default 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, 06:07 AM
Sumit Rajan
Guest
 
Posts: n/a
Default 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, 06:07 AM
Bob Smith
Guest
 
Posts: n/a
Default 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, 06:07 AM
Martijn Lievaart
Guest
 
Posts: n/a
Default 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, 06:08 AM
Default User
Guest
 
Posts: n/a
Default 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, 06:08 AM
Claudio Puviani
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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.