请教一个关于多态性的问题!!
下面的程序的运行结果为什么会是那样的,哪位大虾能给我说说吗?谢谢!
# include <iostream.h>
class A
{
public:
virtual void call(){cout<<"A::call()\n\n"<<endl;}
};
class B:public A
{
public:
void func()
{
cout<<"B::func()"<<endl;
call();
}
virtual void call(){cout<<"B::call()\n\n"<<endl;}
};
class C:public B
{
public:
virtual void call(){cout<<"C::call()\n\n"<<endl;}
};
void main(void)
{
C my_c;
C *mcp=new C;
B my_b;
cout<<"#1 testing"<<endl;
my_c.func();
cout<<"#2 testing"<<endl;
((B *)(&my_c))->func();
cout<<"#3 testing"<<endl;
mcp->func();
cout<<"#4 testing"<<endl;
my_b.func();
}
运行结果是:
#1 testing
B::func()
C::call()
#2 testing
B::func()
C::call()
#3 testing
B::func()
C::call()
#4 testing
B::func()
B::call()