好的,我有一个非常简单的任务。
我得到了类 Person 的这两个构造函数:
Person( const string &, const string &, const string & );
Person( const string &, const string &, const string &,
const string & );
我有 4 个默认值
其中哪些将成为默认构造函数?它总是争论最多的那个吗?或者它是如何工作的?
最佳答案
根据C++标准
4 A default constructor for a class X is a constructor of class X that can be called without an argument.
从您的帖子中不清楚您所说的默认值是什么。您的声明都不是默认构造函数。
如果您在声明中谈论默认参数
Person( const string & = "", const string & = "", const string & = "",
const string & = "" );
然后这个声明是默认构造函数的声明,因为它可以在没有任何显式指定参数的情况下被调用。
有趣的是,同一个构造函数可以同时是默认构造函数和非默认构造函数。至少 C++ 标准没有说任何禁止这样做的内容。
例如
struct A
{
A( int x );
int x;
};
A a1; // error: there is no default constructor
A::A( int x = 0 ) : x( x ) {}
A a2; // well-formed there is a default constructor.
关于c++ - 两个构造函数,哪个是默认的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22301787/