【问题标题】:Is copy constructor called when the temporary returning object is assigned to another object immediately当临时返回对象立即分配给另一个对象时是否调用复制构造函数
【发布时间】:2014-04-04 06:01:16
【问题描述】:

我无法理解这段代码的输出是这样的:

#include<iostream>
using namespace std;

class three_d
{float x,y,z;
public:
    three_d(){x=y=z=0;cout<<"\n\tDefault Constructor";}
    three_d(float a,float b,float c){x=a;y=b;z=c;cout<<"\n\tOverloaded Constructor";}
    three_d(const three_d &obj){x=obj.x;y=obj.y;z=obj.z;cout<<"\n\tCopy Constructor";}
    three_d operator+(three_d op2);
    three_d operator=(three_d op2);
    void show();
    ~three_d(){cout<<"\n\tDestructor Working !!";}
};

three_d three_d::operator+(three_d op2)
{
    x=x+op2.x;
    y=y+op2.y;
    z=z+op2.z;
    return *this; 
}

three_d three_d::operator=(three_d op1)
{
    x=op1.x;
    y=op1.y;
    z=op1.z;
    return *this;
}

void three_d::show()
{
    cout<<"\n\n\tValues are : x="<<x<<" y="<<y<<" and z="<<z<<"\n";
}

int main()
{
    three_d ob1(2.1,2,2.2);
    three_d ob2(1,1,1), ob3;
    ob3=ob1+ob2;
    ob1.show();
    ob2.show();
    ob3.show();

    return 0;
}

输出如下:

1. Overloaded Constructor 
2. Overloaded Constructor
3. Default Constructor
4. Copy Constructor
5. Copy Constructor
6. Destructor Working !!
7. Copy Constructor
8. Destructor Working !!
9. Destructor Working !!
10. Values are : x=3.1 y=3 and z=3.2
11. Values are : x=1 y=1 and z=1
12. Values are : x=3.1 y=3 and z=3.2
13. Destructor Working !!
14. Destructor Working !!
15. Destructor Working !!

所以我的问题是:

  • 在输出的第 5 行,这个复制构造函数是干什么用的?是 *this 指针吗(但因为它是一个指针,我认为它不需要复制构造函数)?第四行是对象 op2(我想)?
  • 如果重载“=”时上述语句为真,为什么只使用一个复制构造函数(当它也有返回值时?
  • 临时(返回)对象何时被破坏?
  • 请解释输出是如何排序的。
  • 即使我把重载的“+”改成这样:

    three_d three_d::operator+(three_d op2)
    {三天温度;
    temp.x=x+op2.x;
    temp.y=y+op2.y;
    temp.z=z+op2.z;
    返回温度;
    }

“=”的复制构造函数的输出保持不变(尽管值会改变),即“=”只有 1 个复制构造函数。但是我认为必须有 2 个复制构造函数用于 "=" 1 用于 op1 对象和其他用于 *this 。

如果我在 main 中使用简单的赋值,例如:
ob3=ob1;
复制构造函数被调用两次。
请解释一下。

【问题讨论】:

  • 空白是你的朋友。
  • 为什么不在重载的运算符中添加一些打印来帮助计算流程?
  • 另外,你应该用换行符结束你的消息,而不是开始它们。
  • 正如@Rsahu 的回答中的cmets 所述,重载+ 以便修改其左侧参数是一个可怕 的想法。 + 通常应该创建并返回一个与其任一操作数分开的新值。
  • this 是一个指针。 *this 是一个对象。

标签: c++


【解决方案1】:

理解该陈述的最佳方式,我认为您应该将其视为;

ob3.operator=(ob1.operator+(ob2));

4. Copy Constructor: 这是从ob2 创建一个临时对象,因为您按值调用operator+。让我们称之为临时 tmp2。

5. Copy Constructor : 这是从ob1 创建一个临时对象,因为您是从operator+ 按值返回的。让我们称之为临时tmp1。

6. Destructor Working !! : 当operator+ 完成时,临时对象 tmp2 被破坏。

--&gt;Important note here : VC++ 和 GCC 编译器有趣地将 operator+ 返回的临时对象 tmp1 通过引用传递给 operator= 甚至 operator= 按值输入(复制)。这就是为什么您在这里看不到另一个复制构造函数的原因。 【这个需要专家解释】

7. Copy Constructor : 这是从ob3 创建另一个临时的,因为您从operator= 按值返回。让我们称之为最后一个临时tmp3。

8. Destructor Working !! : 当operator+ 完成时,tmp1 被破坏。

9. Destructor Working !! : 当operator+ 完成时,tmp3 在分配结束时被破坏。

当我看到GCC 编译器的结果时,如下所示:

4.复制构造函数
5.复制构造函数
6.复制构造函数
7.析构函数工作!!
8.析构函数工作!!
9.析构函数工作!!

我做过的类似分析如下:

  1. tmp2 创建
  2. tmp1 创建
  3. tmp3 创建
  4. tmp3 销毁
  5. tmp1 销毁
  6. tmp2 销毁

注意:我们假设您知道使用签名定义 operator+ 和 operator= 的真实方法。(一个是接受两个常量参数的友元函数,其他通过引用返回等)

【讨论】:

    【解决方案2】:

    operator+ 和 operator= 都返回对象。它们是*this 的副本。因此,对复制构造函数的调用。

    如果想避免调用复制构造函数,为了避免复制成本,请将那些函数更改为返回引用。

    three_d const& three_d::operator+(three_d op2)
    {
       x=x+op2.x;
       y=y+op2.y;
       z=z+op2.z;
       return *this; 
    }
    
    three_d const& three_d::operator=(three_d op1)
    {
       x=op1.x;
       y=op1.y;
       z=op1.z;
       return *this;
    }
    

    附言

    您的 operator+ 函数修改了操作数的 LHS。这是不常见的。在表达式中

    a = b + c;
    

    您不希望 b 被更改。这是需要考虑的事情。

    如果您想以良好的方式实现这些功能,您应该将它们更改为:

    three_d three_d::operator+(three_d const& rhs) const
    {
       three_d r(*this);
       r.x += rhs.x;
       r.y += rhs.y;
       r.z += rhs.z;
       return r; 
    }
    
    three_d& three_d::operator=(three_d const& rhs)
    {
       if ( this != &rhs )
       {
          x = rhs.x;
          y = rhs.y;
          z = rhs.z;
       }
       return *this;
    }
    

    【讨论】:

    • operator+ 不应修改或 return *this (通过引用或其他方式)对于像这样的值语义类型表面上是这样的:它应该按值返回一个临时值。 operator= 通常应该返回一个 non-const 对*this* 的引用,因此可以进一步修改,因为这是为内置类型提供的语义。跨度>
    • 这通常是一个坏的想法。您已定义 + 来更改其左侧参数,因此它的行为类似于 += 而不是 +。正是这样的代码让 Java 设计人员相信,根本不允许运算符重载会更好。
    • @JerryCoffin 因为没有人可以编写一个 .add() 方法来做同样的事情......对不起,我只是认为“它可以做一些你没想到的事情”的论点是愚蠢的。如果您知道该语言支持运算符重载,则必须将a + b 视为方法调用。这是唯一的区别。
    • @clcto:虽然我同意,但简单的事实是:1)这种超载确实是一个糟糕的想法,2)这确实是他们引用的一个原因,为什么最好不要允许重载。
    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2015-12-05
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多