How to pass a method as callback to another class?

后端 未结 4 634
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 01:10

I have a question regarding callbacks using tr1::function. I\'ve defined the following:

  class SomeClass {
    public:
      typedef std::tr1::function

        
4条回答
  •  难免孤独
    2021-01-26 02:01

    Why all this complicated mumbo-jumbo?

    Why not create a class as thus (for example)

    Class MouseOverEventCallBack
    {
       public:
          virtual void RunMouseOverCallback() = 0;
    };
    

    Then just create classes that inherit this class (and redefine the method RunMouseOverCallback)

    Then Register function just needs to be

    void registerCallback(MouseOverEventCallBack *callbackObject); // possible could use a reference
    

    The register method will just call the method and the object will have all that it needs.

    Seems a bit simpler. Let the compiler do the work with pointers to functions etc.

提交回复
热议问题