c++ - 两个构造函数,哪个是默认的?

标签 c++ class constructor

好的,我有一个非常简单的任务。

我得到了类 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/

相关文章:

python - 如何通过方法使用 numba.jit

c++ - C++中继承后如何重写基类的成员

时间:2019-03-08 标签:c++sizeof(string)

c++ - CMake 正在删除预处理器定义

c++ - 这里的 UserArray 是什么意思?

jquery - 删除选定的类 jquery

C++ UDP 在一个类中接收一个 vector

python - 如何在 Flask 中为每个用户 session 维护一个类实例?

c++ - 如何强制调用 move 构造函数(C++)?

Java:我正在尝试使用我创建的名为 Date 的类中的方法显示日期,但是当我尝试将其运行到另一个类中时,数字将不会显示