参考了:http://xingyunbaijunwei.blog.163.com/blog/static/76538067201221010412519/
#include<iostream>
using namespace std;class A{
public:
virtual void f(){ cout<<"A::f()"<<endl;}
void f() const {cout<<"A::f() const"<<endl;}
virtual ~A() {}
};
class B:public A{
public:
void f(){cout<<"B::f()"<<endl;}
void f() const {cout<<"B::f() const"<<endl;}
virtual ~B(){}
};
void g(const A* a)
{
a->f();
}
int main()
{
A *p=new B();
p->f();
g(p);
delete (p);
return 1;
}
输出结果为:
B::f()
A::f() const