C.140: Do not provide different default arguments for a virtual function and an overrider

C.140:不要为虚函数和它的覆盖函数设定不同的默认参数

 

Reason(原因)

That can cause confusion: An overrider does not inherit default arguments.

这可能会引起混乱:覆盖函数不会继承默认参数。

 

Example, bad(反面示例)

 

class Base {public:    virtual int multiply(int value, int factor = 2) = 0;    virtual ~Base() = default;};
class Derived : public Base {public: int multiply(int value, int factor = 10) override;};
Derived d;Base& b = d;
b.multiply(10); // these two calls will call the same function butd.multiply(10); // with different arguments and so different results

 

Enforcement(实施建议)

Flag default arguments on virtual functions if they differ between base and derived declarations.

如果基类和派生的虚函数和覆盖函数的默认参数不同,进行提示。

 


 


 


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