C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C运算符重载和复制构造函数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我很难绕过以下内容(特别是方案b):
(假设我已经定义了赋值运算符,加法运算符和复制构造函数,只是为了输出它们被调用的事实)

情景a:

Simple a;
Simple b;
Simple c = a + b;

    The output is as follows:
    Simple constructor called
    Simple constructor called
    Simple add operator call
    Simple constructor called
    copy constructor called

– 这一切都很好,花花公子

方案b(我无法理解的行为):

Simple d;
Simple e;
Simple f;
f = d + e;

    Simple constructor called
    Simple constructor called
    Simple constructor called
    Simple add operator called
    Simple constructor called
    copy constructor called
    assignment operator called

我的问题是在方案b中,为什么在赋值运算符之前调用复制构造函数是正确的?根据我的理解,只会在未初始化的对象上调用复制构造函数.但是,在这种情况下,对象f已在添加之前的行中初始化.

非常感谢您的解释.

抱歉没有立即发布源代码(并且缺少缩进 – 我在复制到textarea时遇到问题).这就是它的简单性.
我正在使用Visual studio 2005.不幸的是,我还不熟悉它的工作原理,因此我无法指定传递给编译器的优化参数.

class Simple
{
public:
    Simple(void);
Simple operator +(const Simple& z_SimplE) const;
Simple& operator =(const Simple& z_SimplE);
Simple(const Simple& z_Copy);
int m_Width;
int m_Height;
public:
~Simple(void);
};


#include "Simple.h"
#include <iostream>

using std::cout;
using std::endl;

Simple::Simple(void)
{
this->m_Height = 0;
this->m_Width = 0;
cout << "Simple constructor called" << endl;
}

Simple::Simple(const Simple& z_Copy)
{
cout << "copy constructor called" << endl;
this->m_Height = z_Copy.m_Height;
this->m_Width = z_Copy.m_Width;
}

Simple& Simple::operator =(const Simple &z_SimplE)
{
cout << "assignment operator called" << endl;
this->m_Height = z_Simple.m_Height;
this->m_Width = z_Simple.m_Width;   
return *this;
}


Simple Simple::operator +(const Simple &z_SimplE) const
{
cout << "Simple add operator called" << endl;
int y_Height = this->m_Height + z_Simple.m_Height;
int y_Width = this->m_Width + z_Simple.m_Width;
Simple y_Ret;
y_Ret.m_Height = y_Height;
y_Ret.m_Width = y_Width;
return y_Ret;
}

Simple::~Simple(void)
{
cout << "destructor called" << endl;
}

当然Nemo的解释是我的新手C心灵可以掌握的:)

在将优化级别更改为/ O2之后,我可以看到方案b的输出如下(以及我预期的结果)

Simple constructor called
    Simple constructor called
    Simple constructor called
    Simple add operator called
    Simple constructor called
    assignment operator called

谢谢大家的建议.

解决方法

您的运算符按值返回一个对象,如果编译器没有 elide,则可能导致调用复制构造函数.
Simple Simple::operator +(const Simple &z_SimplE) const
{
    //......
    Simple y_Ret;
    //......
    return y_Ret;
}

码:

Simple d;
Simple e;
Simple f;
f = d + e;

这是一步一步的分析:

Simple constructor called     ---> creation of `d`
Simple constructor called     ---> creation of `e`
Simple constructor called     ---> creation of `f`
Simple add operator called    ---> Inside Addition operator
Simple constructor called     ---> creation of local `y_Ret`
copy constructor called       ---> `y_Ret` returned by value 
assignment operator called    ---> Result returned by `+` used for `=`

大佬总结

以上是大佬教程为你收集整理的C运算符重载和复制构造函数全部内容,希望文章能够帮你解决C运算符重载和复制构造函数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。