C++的问题:使用基类成员函数的指针产生多态性,对输出结果不理解
#include <iostream>using namespace std;
class base{
public:
virtual void print(){cout<<"In Base"<<endl;}
};
class derived:public base{
public:
void print(){cout<<"In Derived"<<endl;}
};
void display(base *pb,void (base::*pf)())
{(pb->*pf)();}
void main(){
derived d;
base *pb=&d;
void (base::*pf)();
pf=base::print;
display(pb,pf);
}
就这个程序,我以为它会出现“In Base”的,结果它给我看“In Derived”。不能理解,请求指点。谢谢!