我很难理解以下内容(特别是情况b):
(假设我定义了一个赋值运算符、加法运算符和复制构造函数,只是为了输出它们被调用的事实)
情况a:
我想问的是,在场景b中,为什么复制构造函数在赋值运算符之前被调用?据我所知,复制构造函数只会在未初始化的对象上被调用。然而,在这种情况下,对象f已经在前面一行被初始化了。
您的解释将不胜感激。
很抱歉没有立即发布源代码(以及缺少缩进-我无法将其复制到文本区域中)。 这是它所有简单的代码。 我正在使用Visual Studio 2005。 不幸的是,我对它的工作原理不太熟悉,因此我无法指定传递给编译器的优化参数。
当然,Nemo的解释是我这个初学者可以理解的 :)
将优化级别更改为/O2后,我可以看到场景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已经在前面一行被初始化了。
您的解释将不胜感激。
很抱歉没有立即发布源代码(以及缺少缩进-我无法将其复制到文本区域中)。 这是它所有简单的代码。 我正在使用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的解释是我这个初学者可以理解的 :)
将优化级别更改为/O2后,我可以看到场景b的输出如下(这也是我预期的)
Simple constructor called
Simple constructor called
Simple constructor called
Simple add operator called
Simple constructor called
assignment operator called
感谢大家提供的建议。
+
。除非你公布代码,否则任何解释都只是猜测。 - Alok SaveSimple f; f = d+e;
编译为Simple f; Simple temp = d+e; f = temp;
。(事实上,这是我猜测正在发生的情况。)但是,如果没有看到完整的代码并知道您使用的编译器和优化标志,就无法确定。 - Nemo+=
运算符来解决这个“问题”。 - real4x-fno-elide-constructors
来重现它。 - Kerrek SB