小编Dan*_*lor的帖子

为什么复制构造函数在C++中调用语句d1 = d2 + d3?

我是C++的初学者,我正在使用的资源说下面的语句d3 = d1 + d2; 调用以下内容:

  • +运算符
  • 默认构造函数
  • 复制构造函数
  • 析构函数
  • 分配操作员
  • 析构函数

我不明白为什么在将结果分配给先前声明的变量时调用复制构造函数以及为什么调用2个构造函数.

运营商如下:

date& date::operator=(const date& other)
{
cout << "Date assignment op" << endl;
    if (this!=&other){
        day=other.day;
        month=other.month;
        year=other.year;
    }
    return *this;
}

date date::operator+(const date& other) const
{
    cout << "Date Operator + called" << endl;
    date temp;
    temp.day=day+other.day;
    temp.month=month+other.month;
    temp.year=year+other.year;
    return temp;
}
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

3
推荐指数
1
解决办法
229
查看次数

标签 统计

c++ ×1

copy-constructor ×1