class A
{
public: A(){};
public:
virtual test()
{
cout<<"virtual A"<<endl;
}
};
class B:public A
{
public: B(){};
public:
virtual test()
{
cout<<"virtual B"<<endl;
}
};
int main()
{
A*p=new B();
//cout<<p->test()<<endl;
p->test();
return 0;
}
显示:virtual B
修改之后:
class A
{
public: A(){};
public:
test()//virtual
{
cout<<"virtual A"<<endl;
}
};
class B:public A
{
public: B(){};
public:
test()//virtual
{
cout<<"virtual B"<<endl;
}
};
int main()
{
A*p=new B();
//cout<<p->test()<<endl;
p->test();
return 0;
}
显示:virtual A
前面一种就是因为虚函数的原因,后面的一种没有虚函数,p是指向A类的指针....话不多说理解一下....