C++核心准则C.128:虚函数应该明确定义为virtual,overide或者final

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

C.128:虚函数应该明确定义为virtual,overide或者final

 

 

Reason(原因)

Readability. Detection of mistakes. Writing explicit virtual, override, or final is self-documenting and enables the compiler to catch mismatch of types and/or names between base and derived classes. However, writing more than one of these three is both redundant and a potential source of errors.

可读性。检出错误。明确virtual,overide或者final本身就是一种说明,而且让编译器可以捕捉派生类和基类之间的类型或名称的不匹配问题。然而,使用这三个中的两个或三个就是多余的而且容易引发潜在的错误。

 

It's simple and clear(下面的规则简单又明快):

  • virtual means exactly and only "this is a new virtual function."

  • virual明确表示而且只用于表示"这是一个新的虚函数"

  • override means exactly and only "this is a non-final overrider."

  • overide明确表示而且只表示“这不是最终覆盖者”

  • final means exactly and only "this is a final overrider."

  • final明确表示而且只用于表示“这是最终覆盖者”

 

 

Example, bad(反面示例)

struct B {
    void f1(int);
    virtual void f2(int) const;
    virtual void f3(int);
    // ...
};

struct D : B {
    void f1(int);        // bad (hope for a warning): D::f1() hides B::f1()
    void f2(int) const;  // bad (but conventional and valid): no explicit override
    void f3(double);     // bad (hope for a warning): D::f3() hides B::f3()
    // ...
};

 

Example, good(范例)

struct Better : B {
    void f1(int) override;        // error (caught): Better::f1() hides B::f1()
    void f2(int) const override;
    void f3(double) override;     // error (caught): Better::f3() hides B::f3()
    // ...
};

 

Discussion(讨论)

We want to eliminate two particular classes of errors:

我们希望排除两类特殊的错误:

  • implicit virtual: the programmer intended the function to be implicitly virtual and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly virtual but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be virtual but it is (because it happens to have the same signature as a virtual in the base class)

  • 隐式虚函数:程序员期待函数隐式成为虚函数而且它真的就是了(但是代码的读者看不出来);或者程序员期待函数隐式成为虚函数但是它不是(例如因为参数列表的微小出入);或者程序员没有期待函数成为虚函数但是它是了(因为它恰巧拥有和基类的某个虚函数相同的签名)

  • implicit override: the programmer intended the function to be implicitly an overrider and it is (but readers of the code can't tell); or the programmer intended the function to be implicitly an overrider but it isn't (e.g., because of a subtle parameter list mismatch); or the programmer did not intend the function to be an overrider but it is (because it happens to have the same signature as a virtual in the base class -- note this problem arises whether or not the function is explicitly declared virtual, because the programmer may have intended to create either a new virtual function or a new nonvirtual function)

  • 隐式覆盖:程序员期待函数隐式成为覆盖函数而且它真的就是了(但是代码的读者看不出来);或者程序员期待函数隐式成为覆盖函数但是它不是(例如因为参数列表的微小出入);或者程序员没有期待函数成为覆盖函数但是它是了(因为它恰巧拥有和基类的某个虚函数相同的签名--注意这些问题无论函数是否被明确地声明为虚函数都会发生,因为程序员本来想生成的即可能是虚函数,也可能是非虚函数)

 

 

Enforcement(实施建议)

  • Compare virtual function names in base and derived classes and flag uses of the same name that does not override.

  • 比较基类和派生类的虚函数名称并且提示使用相同名称但不是override的情况。

  • Flag overrides with neither override nor final.

  • 提示没有声明为override或者finald的覆盖函数。

  • Flag function declarations that use more than one of virtual, override, and final.

  • 提示使用virtual,override,final三个关键词的两个或三个的函数声明。

 

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final


 

觉得本文有帮助?欢迎点赞并分享给更多人。

阅读更多更新文章,请关注微信公众号【面向对象思考】

你可能感兴趣的:(C++)