Re: Member function pointer to member function of another class
On 3 avr, 16:33, Praetorian <ashish.sadanan...@gmail.com> wrote:
On Apr 3, 2:38 am, James Kanze <james.ka...@gmail.com> wrote:
[...]
Your suggestion about providing
a functional object to handle indirection is interesting; sounds like
that's what I need. Could you please elaborate on this further or
point me to where I can find more information on this? I really
appreciate your help.
I'm not sure if it's ever really been documented as a pattern;
it was widespread before patterns became widely known.
Basically, your class defines an abstract base class:
class AbstractCallback
{
public:
virtual AbstractCallback() {}
virtual void operator()() const = 0 ;
} ;
and the function the client calls looks something like:
void registerCallback( AbstractCallback const& cb ) ;
The client then derives from AbstractCallback, and passes an
instance of the derived class.
For convenience, you could even provide a template for the
concrete class:
template< typename T, void (T::*fnc)() >
class Callback : public AbstractCallback
{
public:
explicit Callback( T* object )
: myObj( object )
{
}
virtual void operator()() const
{
(myObj->*fnc)() ;
}
private:
T* myObj ;
} ;
The user then creates an instance of Callback< MyClass,
&MyClass::whatever >( this ), and passes you it. (Long before
templates, we used a macro for the derived class.)
Alternatively, your function could be a template, taking a
functional object. But I rather think you'd have problems
bundling that into a DLL.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34