01 January 2023

C++ 核心指南目录

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

理由

容易引起误解:一个重写( override )的函数没有继承默认参数。

例子

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 {return value * factor; };
};

int main()
{
    Derived d;
    Base& b = d;

    cout << b.multiply(10) << endl;  // these two calls will call the same function but
    cout << d.multiply(10) << endl;  // with different arguments and so different results
}
20
100

强化

  • 标注基类和派生类声明的虚函数的默认参数不同的情况