Store Function Pointers to any Member Function

前端 未结 4 1536
太阳男子
太阳男子 2020-12-30 16:59

My Event Manager

For a event manager I need to store many pointers to functions in a vector to call them when the event is triggered. (I will provide the source co

4条回答
  •  情歌与酒
    2020-12-30 17:07

    You can use functors to achieve this. If you wrap a functor around your member functions you can make a vector out of functors. A functor looks like this:

    template  class MyFunctor
    {
    private:
        T* ObjectPtr;
        void (T::*MemberFunction) ();
    public:
        void operator () ()
        {
            return (*this->ObjectPtr.*this->MemberFunction)();
        }
    };
    

    So basically a functor overrides the () operator and returns the member function stored in the functor class. Functors can be quite complex if you want them to work with different signatures but in this article you can get further information.

    http://www.codeproject.com/Articles/7112/Pointers-to-Member-Functions-and-Functors

提交回复
热议问题