Hello! I need to pass a pointer to the member function of the object as an argument to the function. I know how to pass a pointer to a normal function, but not to an object function. Tell me please!

  • one
    Here something is written + many different links. Learn. - avp
  • one
    And here, too, something . - VladD

1 answer 1

Most likely, it is necessary to transfer to the function not only the address of the member function, but also the address of the object; otherwise, inside the function where the pointer is passed, it will not be possible to use this pointer. So with the transfer of two pointers to an object and a member function, it will be something like this:

class A { public: int memFunc(char c) { return c*5; } }; // задаем тип указателя на функцию член: int (A::*MFunc)(char); // MFunc - тип указателя void func(A *po, MFunc pf) { int result = (po->*pf)(10); // вызов функции-члена через указатель на неё } int main (int argc, char** argv) { A a; MFunc pFunc = &A::memFunc; // получили адрес указателя func(&a, pFunc); // передаем его в фунцию func } 
  • Thank! It helped! - user26699