指向成员函数的指针

c++ - How do I typedef a function pointer with the C++11 using syntax? - Stack Overflow

#include <iostream>

class A {
public:
    void display() { std::cout << "A display" << std::endl; }
};

int main() {
    typedef void (A::*PF1)();
    PF1 pf1 = &A::display;
    A a1;
    (a1.*pf1)();

    using PF2 = void (A::*)();
    PF2 pf2 = &A::display;
    A* a2 = new A;
    (a2->*pf2)();
    return 0;
}

// output:
// A display
// A display
posted @ 2023-08-15 14:44  devin1024  阅读(17)  评论(0)    收藏  举报