Requiring virtual function overrides to use override keyword

后端 未结 4 1915
孤独总比滥情好
孤独总比滥情好 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

    GCC and Clang are covered by other answers. Here's same for VC++ from my other answer:

    Below are the relevant warning numbers in VC++:

    C4263 (level 4) 'function': member function does not override any base class virtual member function
    C4266 (level 4) 'function': no override available for virtual member function from base 'type'; function is hidden
    

    To enable these two warnings, you can use one of following options:

    1. Set warning level to 4 in project settings and then disable the warnings you don't want. This is my prefered way. To disable unwanted Level 4 warnings, go to project settings > C/C++ > Advanced and then enter warning numbers in Disable Specific Warnings box.
    2. Enable above two warnings using code.

      #pragma warning(default:4263)
      #pragma warning(default:4266)
      
    3. Enable above two warnings in project settings > C/C++ > Command Line and then enter /w34263 /w34266. Here /wNxxxx option means enable xxxx warnings in Level N (N = 3 is default level). You can also do /wdNxxxx which disables the xxxx warning in level N.

提交回复
热议问题