10

我在玩 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 是使用 move 构造函数构造的。但是,接下来的两行是最令人困惑的:我现在有一个新地址,它不同于 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”一样吗? 现在它不同了,这意味着它在内存中的位置与“a”完全不同,那为什么不调用复制构造函数呢?

希望我的问题清晰易懂。

-------------------------------------------------- -----------------------------------------

编辑:我使用的编译器是带有 -fno-elide-constructors 标志的 clang++。

4

2 回答 2

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 标志的错误。

通过在两个原始成员之后添加一个 int 成员,结果相同,但如果 int 是第一个,则内存损坏!并且非损坏版本以主 ArrayWrapper 中的 nullptr 值结束。查看“和删除”日志以捕获错误行为。

http://coliru.stacked-crooked.com/a/f388c504b442b71d <- int after,好的

http://coliru.stacked-crooked.com/a/9beced1d5a2aa6e4 <- int 之前,腐败转储

于 2014-01-30T21:49:11.050 回答
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

http://coliru.stacked-crooked.com/a/377959ae1e93cdc9

于 2014-01-30T21:51:32.220 回答