Search code examples
c++c++11standardsstandards-compliance

Override two methods at once


The code below surprisingly compiles in VS 2012.

Method C::f() overrides methods in both base classes.

Is this standard behavior? I looked into C++11 standard, and didn't find any explicit mentioning of such situation.

class A { virtual void f() = 0; };

class B { virtual void f() = 0; };

class C : public A, public B { 
  virtual void f() override { } 
};

Solution

  • Yes. The standard says, in C++11 10.3/2

    If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name [etc.] as Base::vf is declared, then [...] it overrides Base::vf.

    There are no special cases for multiple base classes, so a function declared in the derived class will override a suitable function in all base classes.