一段C++代码,可能颠覆你已有的概念
class AD
{
public:
AD() {}
virtual ~AD() {}
void fun()
{
cout << "AD::fun()"<< endl;
}
};
class BD : public AD
{
private:
int i;
public:
BD() { i = 0; }
void fun()
{
i = 1;
cout << "BD::fun()" << endl;
}
};
class CD : public AD
{
public:
CD() {}
void fun2()
{
cout << "CD::fun2()" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
//TestTemp<float, float, int> test;
BD* pBD = new BD();
AD* pAD = dynamic_cast<AD*>(pBD);
CD* pCD = dynamic_cast<CD*>(pAD);
//pBD->fun();
pCD->fun2();
//BD* pBD2 = 0;
//pBD2->fun();
return 0;
}
pCD为空,但是依旧可以输出,调用fun2函数。