可能重复:
C++ : implications of making a method virtual
Why is 'virtual' optional for overridden methods in derived classes?C++:派生类和虚拟方法
我不知道,什么是在以下情况下记录的行为:
你有
class A
{
virtual void A()
{
cout << "Virtual A"<<endl;
}
void test_A()
{
A();
}
}
class B: public A
{
void A()
{
cout << "Non-virtual A in derived class"<<endl;
}
void test_B()
{
A();
}
}
A a; B b;
a.test_A();
b.test_A();
b.test_B();
它应该按照C++标准做什么,为什么? GCC像B :: A一样也是虚拟的。
当你在派生类中通过非虚拟方法覆盖虚拟方法时,通常会发生什么?
您的代码[不应编译](HTTP: //ideone.com/gI9bI)。 – 2010-11-12 11:16:11
我认为这也是答案在这里:http://stackoverflow.com/questions/2963965/why-is-virtual-optional-for-overridden-methods-in-derived-classes – mkj 2010-11-12 11:18:10