下面是一些旨在显示何时覆盖虚拟方法的代码。它输出: B B A A B A 这是正确的吗?我认为 bar 方法不能被覆盖......?
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo(){cout<<"A"<<endl;}
void bar(){cout<<"A"<<endl;}
};
class B : public A
{
public:
void foo(){cout<<"B"<<endl;}
void bar(){cout<<"B"<<endl;}
};
int main()
{
B b;
A * pA = new A;
A * pA2 = &b;
b.foo(); b.bar();
pA->foo(); pA->bar();
pA2->foo(); pA2->bar();
}