Requiring virtual function overrides to use override keyword

后端 未结 4 1827
孤独总比滥情好
孤独总比滥情好 2020-12-29 01:47

C++11 added override to ensure that member functions you write that you intend to override base-class virtual functions actually do (or won\'t compile).

4条回答
  •  有刺的猬
    2020-12-29 02:08

    There are two things you can do.

    First, Clang 3.5 and higher have a -Winconsistent-missing-override warning (triggered by -Wall). This does not quite work for your example, but only if you would add a void foo() override {} to class B and not in class C. What you actually want is -Wmissing-override, to locate all missing override, not just the inconsistently missing ones. That is currently not provided, but you might complain on the Clang mailinglist and they might add it.

    Second, you use Howard Hinnant's trick to temporarily add final to the base class member function and recompile. The compiler will then locate all further derived classes that attempt to override the virtual base member function. You can then fix the missing ones. It's a bit more work and requires frequent rechecking when your class hierarchy expands.

提交回复
热议问题