Re: static member functions vs. member function pointers
On Apr 29, 8:41 am, paul <pradhan.push...@gmail.com> wrote:
I have seen the use of static member functions for callbacks
by passing the name of the static member function and the this
pointer to the code that will call back.
That's interesting. I've never seen it, at least not in correct
code.
I was thinking the same can be achieved by using member
function pointers and the this pointer.
E.g.
void FuncThatWillCallback( MyClass::staticFunc, (void *)this)
{
staticFunc(this);}
void FuncThatWillCallback( PtrToMemberFunc, (void *)this)
{
this->PtrToMemberFunc;
}
Is there a reason why the static member function trick is so
widely used? (Except when FuncThatWillCallback is part of a
3rd party lib).
As I said, I've never seen it used in correct code, period. In
pure C++, there are two widespread solutions: a callback
interface, using virtual functions, from which the client code
derives, and functional objects passed to a template. About the
only time I've seen anything else has been when interfacing to
legacy API's, in C, and in such cases, you can't pass a static
member function either, because the callback function must have
"C" linkage.
--
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