1 struct A 2 { 3 virtual void f() {tcout << _T("A::f()\n");} 4 }; 5 6 struct B : public A 7 { 8 void f() {tcout << _T("B::f()\n");} 9 }; 10 11 struct C : public B 12 { 13 void f() {tcout << _T("C::f()\n");} 14 }; 15 16 struct D : public C 17 { 18 void f() {tcout << _T("D::f()\n");} 19 }; 20 21 22 int _tmain(int argc, TCHAR * argv[], TCHAR * envp[]) 23 { 24 A * pA = new D; 25 pA->f(); 26 27 return 0; 28 }
结果:D::f()
c++ 基类已经存在virtual修饰的同名成员函数,派生类加不加无所谓,都是virtual的
原文:http://www.cnblogs.com/kidwzc/p/5240226.html