class interaction

B

Bob Smith

Dan said:
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

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

Dan

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
 
A

Attila Feher

Dan said:
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

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).
 
S

Sumit Rajan

Dan said:
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



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.
 
M

Martijn Lievaart

How can I let two classes call a method from each other,
like in this example?

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

void doSomething();
};

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

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
 
C

Claudio Puviani

Dan said:
How can I let two classes call a method from each other,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top