Class member function as function pointer

前端 未结 2 1606
温柔的废话
温柔的废话 2021-01-28 03:24

I have a class and one of its member functions is actually a function pointer. That way the user can overwrite what does this function do. I unfortunately have some difficulties

2条回答
  •  广开言路
    2021-01-28 03:52

    You do not have a class with a "function which is a function pointer", but rather a class with a data member which is a function pointer. This is an important distinction as the function won't receive a this pointer, nor will it have access to protected or private members.

    You've declared your function pointer correctly, but you need to use the .fcn_ptr rather than .*fcn_ptr. I believe this is sufficient to solve your problem.

    Do note that you're using old C style function pointer syntax. You may want to learn the C++ function pointer alternatives including std::function.

提交回复
热议问题