Here is well described how to call member function by pointer:
http://www.newty.de/fpt/functor.html
But the functor needs to get 2 arguments: pointer-to-object and pointer-to-member-function:
TSpecificFunctor(TClass* _pt2Object, void(TClass::*_fpt)(const char*))
{ pt2Object = _pt2Object; fpt=_fpt; }
call:
(*pt2Object.*fpt)(string);
Is it possible to pass single argument like C-style:
func() -- call
func -- function pointer
Why obj.method isn’t complete pointer-to-class-member?
The syntax
object.*ptmfdoesn’t create an intermediate object. It has no meaning and is forbidden by the language. You have to immediately call the result of accessing a pointer to member function.You can explicitly create such an object using
std::bind, which interprets the ptmf as a functor object, and makes the implicitthisargument explicit.http://ideone.com/ds24F
Note that this functionality is new in C++11. Although C++03 TR1 has
functionandbind, they won’t perform this conversion on a ptmf. (Plain C++03 can do the job withstd::mem_fnandstd::bind1st, but they are extremely painful to use and have been deprecated.)