对象的静态类型和动态类型具体怎么 样来定义 ?
看一个例子;
class Base
{
virtual void g(int i= 10)
{
cout<<i<<endl;
}
};
class Derived: public Base
{
virtual void g(int i= 20)
{
cout<<"Derived::g()"<<i<<endl;
}
};
class CThird:public Derived
{
virtual void g(int i=30)
{
cout<<"CThird::g()"<<i<<endl;
}
};
void main(void)
{
Base b;
Derived d;
CThird t;
Base* pb = new Derived;
Derived* pd = new CThird;
b.g(); //10 没问题
d.g(); //20 没问题
t.g(); //30 没问题
pb->g(); //Derived::g()10 可以理解
pd->g(); //CThird::g() 20 ,为什么 不是 10?
delete pb;
delete pd;
}
虚函数的默认参数由对象的静态类型决定?
对于pb 来说 :此时的静态类型指Base.
对于pd来说:静态类型变为Derived.
为什么?
按说CThird也是间接从Base派生来的 呀?