Calling C++ class methods via a function pointer

前端 未结 10 940
醉酒成梦
醉酒成梦 2020-11-22 06:47

How do I obtain a function pointer for a class member function, and later call that member function with a specific object? I’d like to write:

class Dog : A         


        
10条回答
  •  天涯浪人
    2020-11-22 07:23

    Reason why you cannot use function pointers to call member functions is that ordinary function pointers are usually just the memory address of the function.

    To call a member function, you need to know two things:

    • Which member function to call
    • Which instance should be used (whose member function)

    Ordinary function pointers cannot store both. C++ member function pointers are used to store a), which is why you need to specify the instance explicitly when calling a member function pointer.

提交回复
热议问题