C++ Style: Prefixing virtual keyword to overridden methods

后端 未结 6 619
名媛妹妹
名媛妹妹 2020-12-09 03:15

I\'ve been having a discussion with my coworkers as to whether to prefix overridden methods with the virtual keyword, or only at the originating base class.

I tend t

相关标签:
6条回答
  • 2020-12-09 03:21

    I can think of one disadvantage: When a class member function is not overridden and you declare it virtual, you add an uneccessary entry in the virtual table for that class definition.

    0 讨论(0)
  • 2020-12-09 03:24

    I completely agree with your rationale. It's a good reminder that the method will have dynamic dispatch semantics when called. The "that method isn't virtual" argument that you co-worker is using is completely bogus. He's mixed up the concepts of virtual and pure-virtual.

    0 讨论(0)
  • 2020-12-09 03:25

    Adding virtual does not have a significant impact either way. I tend to prefer it but it's really a subjective issue. However, if you make sure to use the override and sealed keywords in Visual C++, you'll gain a significant improvement in ability to catch errors at compile time.

    I include the following lines in my PCH:

    #if _MSC_VER >= 1400
    #define OVERRIDE override
    #define SEALED sealed
    #else
    #define OVERRIDE
    #define SEALED
    #endif
    
    0 讨论(0)
  • 2020-12-09 03:36

    Note: My answer regards C++03 which some of us are still stuck with. C++11 has the override and final keywords as @JustinTime suggests in the comments which should probably be used instead of the following suggestion.

    There are plenty of answers already and two contrary opinions that stand out the most. I want to combine what @280Z28 mentioned in his answer with @StevenSudit's opinion and @Abhay's style guidelines.

    I disagree with @280Z28 and wouldn't use Microsoft's language extensions unless you are certain that you will only ever use that code on Windows.

    But I do like the keywords. So why not just use a #define-d keyword addition for clarity?

    #define OVERRIDE
    #define SEALED
    

    or

    #define OVERRIDE virtual
    #define SEALED virtual
    

    The difference being your decision on what you want to happen in the case you outline in your 3rd point.

    3 - If, through some error, the virtual were removed from IFoo, all children will still function (CFooSpecialization::DoBar would still override CFooBase::DoBar, rather than simply hiding it).

    Though I would argue that it is a programming error so there is no "fix" and you probably shouldn't even bother mitigating it but should ensure it crashes or notifies the programmer in some other way (though I can't think of one right now).

    Should you chose the first option and don't like adding #define's then you can just use comments like:

    /* override */
    /* sealed */
    

    And that should do the job for all cases where you want clarity, because I don't consider the word virtual to be clear enough for what you want it to do.

    0 讨论(0)
  • 2020-12-09 03:40

    A function once a virtual always a virtual.

    So in any event if the virtual keyword is not used in the subsequent classes, it does not prevent the function/method from being 'virtual' i.e. be overridden. So one of the projects that I worked-in, had the following guideline which I somewhat liked :

    • If the function/method is supposed to be overridden always use the 'virtual' keyword. This is especially true when used in interface / base classes.
    • If the derived class is supposed to be sub-classed further explicity state the 'virtual' keyword for every function/method that can be overridden. C++11 use the 'override' keyword
    • If the function/method in the derived class is not supposed to be sub-classed again, then the keyword 'virtual' is to be commented indicating that the function/method was overridden but there are no further classes that override it again. This ofcourse does not prevent someone from overriding in the derived class unless the class is made final (non-derivable), but it indicates that the method is not supposed to be overridden. Ex: /*virtual*/ void guiFocusEvent(); C++11, use the 'final' keyword along with the 'override' Ex: void guiFocusEvent() override final;
    0 讨论(0)
  • 2020-12-09 03:41

    I would tend not to use any syntax that the compiler will allow me to omit. Having said that, part of the design of C# (in an attempt to improve over C++) was to require overrides of virtual methods to be labeled as "override", and that seems to be a reasonable idea. My concern is that, since it's completely optional, it's only a matter of time before someone omits it, and by then you'll have gotten into the habit of expecting overrides to be have "virtual" specified. Maybe it's best to just live within the limitations of the language, then.

    0 讨论(0)
提交回复
热议问题