解决方法
在C 11中,可以使用覆盖标识符:
struct Base { virtual void foo() const { std::cout << "Base::foo!\n"; } }; struct Derived : virtual public Base { virtual void foo() const override {std::cout << "Derived::foo!\n";} };
这允许您在编译时找出是否未能覆盖方法.在这里,我们忽略了使方法const:
struct BadDerived : virtual public Base { virtual void foo() override {std::cout << "BadDerived::foo!\n";} // FAIL! Compiler finds our mistake. };