C++定义 3 个参数,但只使用 2 个

C++ define 3 arguments but using just 2

本文关键字:定义 参数 C++      更新时间:2023-10-16

我想使用不同数量的参数。

class A {
    public:
          A(int a, int b);
};
A::A(int a, int b) {
    // constructor code
}
int main() { 
    A a(5); // I use only 1 argument and the second one I let default ?
}

构造函数是(有点特殊的)函数 - 常规默认参数语法适用。

class A {
    public:
          A(int a, int b = default_value);
};
A::A(int a, int b) {
    // constructor code
}
int main() { 
    A a(5);
}