在类中的使用案例:
#include <iostream> class Method; // forward declaration class MethodPtr { public: void (Method::*func)() = nullptr; }; class Method { public: Method(); void doSomething(); void call(); // will use methodptr MethodPtr methodptr; }; Method::Method() : methodptr{&Method::doSomething} {} void Method::call() { // dereferencing the function pointer to call it on `this`: // auto& p = methodptr.func; // p 指针指向 doSomething 函数地址 // (this->*p)(); (this->*methodptr.func)(); } void Method::doSomething() { std::cout << "Did something!" << std::endl; } int main() { Method method; method.call(); }
结果:
Did something!标签:doSomething,void,call,func,使用,函数指针,Method,methodptr From: https://www.cnblogs.com/strive-sun/p/16992543.html