2

I'm refreshing some forgotten concept about pointer to function to create some kind of wrapper class to deal with callback calls.

After surf in google I found this simple but interesting post http://blog.coldflake.com/posts/C++-delegates-on-steroids/

So far I understand almost everything except this

    T::*TMethod

I can remember the meaning of ::*, you can see that in many part of the post, here is the first time that author use it

class Delegate
{
    typedef void (*Type)(void* callee, int);
public:
    Delegate(void* callee, Type function)
        : fpCallee(callee)
        , fpCallbackFunction(function) {}
    template <class T, void (T::*TMethod)(int)> <<<<<<First time
    static Delegate from_function(T* callee)
    {
        Delegate d(callee, &methodCaller<T, TMethod>);
        return d;
    }
    void operator()(int x) const
    {
        return (*fpCallbackFunction)(fpCallee, x);
    }
private:
    void* fpCallee;
    Type fpCallbackFunction;
    template <class T, void (T::*TMethod)(int)>
    static void methodCaller(void* callee, int x)
    {
        T* p = static_cast<T*>(callee);
        return (p->*TMethod)(x);
    }
};

So far, I understard de concept of how it work but I can't remember the meaning of ::*

I know that propablythisisavery basic and silly question, so be nice, please.

Best Regards

Trungus
  • 165
  • 8

1 Answers1

2

Its a function pointer to a member function of T class.

Simple example:

class A { 
    int func() { return 1; }
public:
    int (A::*pf)();

    A() : pf(&A::func) {}
};

int main() { 
   A obj;
   std::cout << (obj.*obj.pf)();
   return 0;
}
Kaidul
  • 15,409
  • 15
  • 81
  • 150
  • Hi guys, thank you for the answer, I'm reading the suggested post to review some concepts, thank you again for the help – Trungus Oct 15 '16 at 16:47