#include <iostream>
class Base{
public:
virtual int foo( int ival = 1024 )
{
cout<<"Base::foo()----- ival: "<<ival<<endl;
return ival;
}
};
class Derived : public Base
{
public:
virtual int foo( int ival = 2048)
{
cout<<"Derived::foo()----- ival: "<<ival<<endl;
return ival;
}
};
Derived *pd = new Derived;
Base *pb = pd;
pb->foo();//输出结果为:Derived::foo()----- ival: 1024
结论:由于虚拟机制 pb调用的是派生类的成员函数,但是缺省实参的值由基类指定!