class Base { // 基类
public:virtual void f() {cout<<"Base"<<endl;}
};
class Derived : public Base { // 派生类
public:virtual void f() { // 最好加上该virtual cout<<"Derived"<<endl;}
};int main(){Base* p = new Derived();p->f(); // Derived(*p).f(); // Derivedstatic_cast<Base>(*p).f(); // Basedelete p;return 0;
}
通过强制类型转换可以实现