D
Denis Remezov
Ele said:Given class A and class B:
class A
{
public:
void method_for_B_to_call();
...
};
How to make B to call back method_for_B() of A without letting B know the
definition of A (e.g. not using "#include "A.h"), but only know
A::method_for_B_to_call() for calling back?
Thanks!
You cannot do it directly in your definitions. You need to find an
abstraction for class B to use; which exactly - depends on the details
of your problem.
Here are two approaches:
0) Delegation:
//////header (used by class B):
class A; //forward declaration
//used by class B:
class C {
A* pa_;
public:
void method_for_B_to_call();
};
//////.cpp file: includes the above header and the definition of class A
void C::method_for_B_to_call() {
return pa_->method_for_B_to_call();
}
1) Abstract base class (interface) for A:
//used by B, implemented by A
class ABase {
public:
virtual ~ABase() = 0;
virtual void method_for_B_to_call() = 0;
//...
};
Denis