Rediger

Del via


Warning C26435

The virtual function 'symbol' should specify exactly one of 'virtual', 'override', or 'final' (c.128)

C++ Core Guidelines

C.128: Virtual functions should specify exactly one of virtual, override, or final

To improve readability, the kind of virtual behavior should be stated clearly and without unnecessary redundancy. Even though multiple virtual specifiers can be used simultaneously, it's better to specify one at a time to emphasize the most important aspect of virtual behavior. The following order of importance is apparent:

  • plain virtual function;
  • virtual function that explicitly overrides its base;
  • virtual function that overrides its base and provides the final implementation in the current inheritance chain.

Notes

  • This rule skips destructors since they have special rules regarding virtuality.
  • Warnings show up on function definitions, not declarations. It may be confusing, since definitions don't have virtual specifiers, but the warning is still appropriate.

Code analysis name: SINGLE_VIRTUAL_SPECIFICATION

Example: Redundant specifier

class Ellipse : public Shape {
public:
    void Draw() override {
        //...
    }
};

class Circle : public Ellipse {
public:
    void Draw() override final { // C26435, only 'final' is necessary.
        //...
    }
    virtual void DrawCircumference() final { // C26435, should be neither 'virtual' nor 'final'.
        //...
    }
};

See also

C.128: Virtual functions should specify exactly one of virtual, override, or final