在 C++ 中,如果我创建一个构造函数,它接受一个具有默认值的参数——它会用作默认(空)构造函数吗?

In c++, if I create a constructor that takes one argument which has a default value - will that serve as a default (empty) constructor?

下面的单参数构造函数是否也作为默认构造函数?

class SomeClass
    {
    public:
        SomeClass(const int &a = 4);
    }

(假设构造函数定义明确等)

谢谢!

是的,默认构造函数的定义允许参数,只要有默认值即可:

A default constructor for a class X is a constructor of class X for which each parameter that is not a function parameter pack has a default argument (including the case of a constructor with no parameters).

(来自 C++1z 草案)

旧的措辞:

A default constructor for a class X is a constructor of class X that can be called without an argument.


此外,您的复制构造函数将被隐式定义为默认的,因为您还没有声明一个。

没有 "default copy constructor" 这样的东西。但是"default constructor"和"defaulted copy constructor"是有意义的。