1. 程式人生 > >趨勢一道題目,說說const 過載及虛擬函式

趨勢一道題目,說說const 過載及虛擬函式

參考了: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