1
#include <iostream>
using namespace std;
#include <cstring>

class Word{
    private:
        char* ptr = nullptr;
    public:
        Word(){
            cout << "default constructor" << endl;
        }
        Word(const char* sentence):ptr{new char [strlen(sentence)+1]}{
            strcpy(ptr, sentence);
            cout << "conversion constructor: " << ptr << endl;
        }
        Word(const Word& w):ptr{new char [strlen(w.ptr)+1]}{
            strcpy(ptr, w.ptr);
            cout << "copy constructor: "<< ptr << endl;
        }
        ~Word(){
            cout << "destructor: " << ptr << endl;
        }
};

int main(){
    Word a ("A stands for apple!");
    Word&& b = "B stands for Banana, rvalue ref";
    b = a;
}

我的 Eclipse 结果:

conversion constructor: A stands for apple!
conversion constructor: B stands for Banana, rvalue ref
destructor: A stands for apple!
destructor: A stands for apple!

我的期望:

conversion constructor: A stands for apple!
conversion constructor: B stands for Banana, rvalue ref
destructor: B stands for Banana, rvalue ref
destructor: A stands for apple!
destructor: A stands for apple!

我对这一步感到困惑。

b = a;

当a被赋值给b时,它可以假设首先破坏b持有的临时对象(使用cstring“B代表Banana,rvalue ref”),然后将a的值分配给b。为什么在 Eclipse 的结果中,它不执行临时对象的销毁?

4

2 回答 2

4

你的期望是错误的。不能有比建设更多的破坏。

当 a 分配给 b 时,它可以假设首先破坏临时对象

编号b是指临时对象。分配给对象不会导致对象被销毁。

发生的情况是:隐式生成的赋值运算符Word将分配所有成员。因此,在赋值之后, 的前一个值b.ptr被泄露并且具有与 相同的值(指向相同的字符串)a.ptr

于 2018-04-23T13:45:00.167 回答
0

b = a;正在调用赋值运算符,而不是复制构造函数。如果显式删除,代码将无法编译:

// trimmed...
Word(const Word& w):ptr{new char [strlen(w.ptr)+1]}{
    strcpy(ptr, w.ptr);
    cout << "copy constructor: "<< ptr << endl;
}
Word& operator = (const Word& w) = delete;

编译行:

$ g++ rvalue-ref.cpp -o rvalue-ref
rvalue-ref.cpp: In function ‘int main()’:
rvalue-ref.cpp:45:9: error: use of deleted function ‘Word& Word::operator=(const Word&)’
     b = a;
         ^
rvalue-ref.cpp:20:15: note: declared here
         Word& operator = (const Word& w) = delete;
               ^~~~~~~~

编译器将提供一个默认赋值运算符,因此您问题中的代码就是利用它。要查看发生了什么,请添加复制分配和移动分配运算符。

Word& operator = (const Word& w) {
    auto temp = new char [strlen(w.ptr)+1];
    strcpy(temp, w.ptr);
    delete [] ptr;
    ptr = temp;
    cout << "assignment operator: " << ptr << endl;
    return *this;
}
Word& operator = (Word&& w) {
    std::swap(ptr, w.ptr);
    cout << "swap operator: " << ptr << endl;
    return *this;
}

有了这些,我得到了预期的输出:

conversion constructor: A stands for apple!
conversion constructor: B stands for Banana, rvalue ref
assignment operator: A stands for apple!
destructor: A stands for apple!
destructor: A stands for apple!

顺便说一句,您正在泄漏内存。您的析构函数应如下所示:

~Word(){
    cout << "destructor: " << ptr << endl;
    delete [] ptr;
}

$ valgrind ./rvalue-ref

==10736== Memcheck, a memory error detector
==10736== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10736== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10736== Command: ./rvalue-ref
==10736== 
conversion constructor: A stands for apple!
conversion constructor: B stands for Banana, rvalue ref
assignment operator: A stands for apple!
destructor: A stands for apple!
destructor: A stands for apple!
==10736== 
==10736== HEAP SUMMARY:
==10736==     in use at exit: 0 bytes in 0 blocks
==10736==   total heap usage: 5 allocs, 5 frees, 73,800 bytes allocated
==10736== 
==10736== All heap blocks were freed -- no leaks are possible
==10736== 
==10736== For counts of detected and suppressed errors, rerun with: -v
==10736== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

您还可以使用复制/交换习语(如下)来实现赋值运算符。由于临时性,这将添加额外的构造函数/析构函数输出,但总体而言这是一个很好的做法。

Word& operator = (const Word& w) {
    Word temp(w);
    std::swap(ptr, temp.ptr);
    cout << "assignment operator: " << ptr << endl;
    return *this;
}
于 2018-04-23T13:43:20.580 回答