标签:
在了解静态绑定和动态绑定之前,先了解什么是对象的静态类型,什么是对象的动态类型。
class Base
{
public:
void setData(int i=10)
{
cout <<" virtual int Base::setData()"<<endl;
}
virtual int getData()
{
cout <<" virtual int Base::getData()"<<endl;
}
private:
int m_value;
};
class Derive: public Base
{
public:
void setData(int i=20)
{
cout <<" virtual int Derive::setData()"<<endl;
}
virtual int getData()
{
cout <<" virtual int Derive::getData()"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derive *pd = new Derive;//pd的静态类型为Derive,动态类型也为Derive
Base *pb = pd; //pb的静态类型为Base,动态类型为Derive
return 0;
}
Derive *pd = new Derive;
Base *pb = pd;
pb->setData();
pd->setData();
class Base
{
public:
virtual void getData(int i=10)
{
cout <<" virtual int Base::getData()" << i <<endl;
}
};
class Derive: public Base
{
public:
virtual void getData(int i = 20)
{
cout <<" virtual int Derive::getData()" << i <<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derive *pd = new Derive;
Base *pb = pd;
pb->getData();
pd->getData();
return 0;
}
C++ 静态绑定与动态绑定------绝不重新定义继承而来的缺省参数
标签:
原文地址:http://www.cnblogs.com/chengkeke/p/5417349.html