Is it possible to break code by adding a new virtual function in the base class?
问题 Is it possible to have the observed behavior of a program changed by simply adding a new virtual function to a base class? I mean that no other change must be made to the code. 回答1: #include <stdlib.h> struct A { #if ADD_TO_BASE virtual void foo() { } #endif }; struct B : A { void foo() { } }; struct C : B { void foo() { abort(); } }; int main() { C c; B& b = c; b.foo(); } Without the virtual function the base class b.foo() is a non-virtual call to B::foo() : $ g++ virt.cc $ ./a.out With the