指向 class 的成员函数的指针

pointer to a member function of a class

我已经声明了一个指向 class 成员函数的指针。它给出了错误。

#include<iostream>

using namespace std;

class B
{
public:
    int b;
    void get()
    {
        cin>>b;
    }
};

int main()
{
    B b1 ;
    void (B::*ptr)()=&B::get;
    b1.*ptr();
    cout<<b1.b;
 }

成员指针运算符 .*->* 的优先级低于函数调用语法。你需要做的:

(b1.*ptr)();