Reference to non-static member function must be called

前端 未结 2 1470
感情败类
感情败类 2020-12-04 19:37

I\'m using C++ (not C++11). I need to make a pointer to a function inside a class. I try to do following:

void MyClass::buttonClickedEvent( int buttonId ) {
         


        
相关标签:
2条回答
  • 2020-12-04 20:08

    The problem is that buttonClickedEvent is a member function and you need a pointer to member in order to invoke it.

    Try this:

    void (MyClass::*func)(int);
    func = &MyClass::buttonClickedEvent;
    

    And then when you invoke it, you need an object of type MyClass to do so, for example this:

    (this->*func)(<argument>);
    

    http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm

    0 讨论(0)
  • 2020-12-04 20:15

    You may want to have a look at https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs-memfnptr-types, especially [33.1] Is the type of "pointer-to-member-function" different from "pointer-to-function"?

    0 讨论(0)
提交回复
热议问题