我正在玩C ++构造函数。这是我的代码:
#include <iostream>
using namespace std;
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper ()
: _p_vals( new int[ 64 ] )
, _size( 64 )
{
cout << "Default constructor: " << this << endl;
}
explicit ArrayWrapper (int n)
: _p_vals( new int[ n ] )
, _size( n )
{
cout << "Constructor: " << this << endl;
}
// move constructor
ArrayWrapper (ArrayWrapper&& other)
: _p_vals( other._p_vals )
, _size( other._size )
{
cout << "Move constructor: " << this << endl;
cout << "Move from: " << &other << endl;
other._p_vals = NULL;
other._size = 0;
}
// copy constructor
ArrayWrapper (const ArrayWrapper& other)
: _p_vals( new int[ other._size ] )
, _size( other._size )
{
cout << "Copy constructor: " << this << endl;
for ( int i = 0; i < _size; ++i )
{
_p_vals[ i ] = other._p_vals[ i ];
}
}
~ArrayWrapper ()
{
cout << "Destructor: " << this << endl;
delete [] _p_vals;
}
public:
int *_p_vals;
int _size;
};
ArrayWrapper foo() {
ArrayWrapper a(7);
cout << "Temp object created!" << endl;
return a;
}
int main() {
ArrayWrapper b(foo());
cout << "Finish!" << endl;
}
输出结果为:
Constructor: 0x7fff5d97bb60
Temp object created!
Destructor: 0x7fff5d97bb60
Move constructor: 0x7fff5d97bbd0
Move from: 0x7fff5d97bbc0
Destructor: 0x7fff5d97bbc0
Finish!
Destructor: 0x7fff5d97bbd0
前三行表示foo()函数中的局部变量是用构造函数创建的,并在foo()返回时被销毁。第4行表示b是使用移动构造函数构造的。但是,接下来的两行最令人困惑:我现在有一个新地址,这与foo()中的局部变量“a”不同,我曾经称之为移动构造函数。复制构造函数完成后,右值引用将消失,并且将调用析构函数。但为什么不存在0x7fff5d97bbc0的复制构造函数?换句话说,0x7fff5d97bbc0来自何处以及如何构造?简单地说,还有一个被称为构造函数的析构函数被调用。
我感觉这与复制精灵有关。因此我将foo()中的返回行更改为以下内容:
return std::move(a);
输出是:
Constructor: 0x7fff55a7ab58
Temp object created!
Copy constructor: 0x7fff55a7abc0
Destructor: 0x7fff55a7ab58
Move constructor: 0x7fff55a7abd0
Move from: 0x7fff55a7abc0
Destructor: 0x7fff55a7abc0
Finish!
Destructor: 0x7fff55a7abd0
现在它终于有了一些道理:在第三行,它表明在“a”被销毁之前调用了复制构造函数。这意味着,当按值返回时,它实际上会在销毁临时变量之前将值复制到返回值中。
但我仍然对原始程序感到困惑(没有std :: move()),因为如果它确实是由复制省略引起的,那么foo()的返回值的地址不应该与局部变量相同“一个”?既然它是不同的,这意味着它位于内存中与“a”完全不同的位置,那为什么它不调用复制构造函数呢?
希望我的问题清楚易懂。
编辑:我使用的编译器是clang ++和-fno-elide-constructors flag。
答案 0 :(得分:3)
你的编译器是什么,没有std::move
:
Constructor: 0x7fff0b8e3b80
Temp object created!
Finish!
Destructor: 0x7fff0b8e3b80
使用std::move
:
Constructor: 0x7fffca87eef0
Temp object created!
Move constructor: 0x7fffca87ef30
Move from: 0x7fffca87eef0
Destructor: 0x7fffca87eef0
Finish!
Destructor: 0x7fffca87ef30
这两个结果比你的更合乎逻辑,所以你的编译器又是什么?
编辑:它的味道像-fno-elide-constructors标志的bug。
通过在两个原始成员之后添加一个int成员,结果相同,但如果int是第一个,则内存损坏!非损坏版本以主ArrayWrapper中的nullptr值结束。请参阅“和删除”日志以捕获错误行为。
http://coliru.stacked-crooked.com/a/f388c504b442b71d&lt; - int after,ok
http://coliru.stacked-crooked.com/a/9beced1d5a2aa6e4&lt; - int之前,腐败转储
答案 1 :(得分:0)
看起来你有很多副本,使用g ++ 4.8我得到以下输出:
Constructor: 0x7fff88925df0
Temp object created!
Finish!
Destructor: 0x7fff88925df0
如果我添加:-fno-elide-constructors然后我得到:
Constructor: 0x7fff1bd329b0 for this line: ArrayWrapper a(7);
Temp object created!
Move constructor: 0x7fff1bd329f0 for temporary
Move from: 0x7fff1bd329b0 moving from this : ArrayWrapper a(7)
Destructor: 0x7fff1bd329b0 foo is ending so destroy: ArrayWrapper a(7);
Move constructor: 0x7fff1bd329e0 for ArrayWrapper b - it is being created
Move from: 0x7fff1bd329f0 moving from temporary
Destructor: 0x7fff1bd329f0 destroying temporary
Finish!
Destructor: 0x7fff1bd329e0 destroy object b