2015-08-25 150 views
-6

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

#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; 
} 

回答

3

指针到构件运营.*->*具有比函数调用的语法的优先级低。您需要做:

(b1.*ptr)();