首先OP的代碼進行審查。我稍微修改了一下,以便更明顯地發生。
#include <iostream>
using namespace std;
static int allocCounter = 0;
class TestCopy
{
private:
string pStr;
int counter;
public:
TestCopy(const TestCopy &obj)
{
allocCounter++;
counter = allocCounter;
cout << "copy construct " << counter << endl;
pStr = obj.pStr;
}
TestCopy(const string &test)
{
allocCounter++;
counter = allocCounter;
cout << "string construct " << counter << endl;
pStr = test;
}
~TestCopy()
{
cout << counter << "\t" << pStr << endl;
}
TestCopy get()
{
TestCopy x = *this; // copy constructed
return TestCopy(x); // copy constructed and copy elision
}
string getStr()
{
return pStr;
}
TestCopy & operator=(const TestCopy &obj)
{
cout << "assigned " << obj.counter << " to "<< counter << endl;
pStr = obj.pStr;
// counter = obj.counter; deliberately left out
return *this;
}
};
int main()
{
string xstr = "test";
TestCopy x(xstr); // string constructed
TestCopy x2 = x.get(); // Would be a copy constructed if not for copy elision
return 0;
}
輸出
string construct 1
copy construct 2
copy construct 3
2 test
3 test
1 test
注意即使TestCopy x=*this;
好吧賦值操作符的呼叫。現在我們該如何砍掉一些呢?
首先我們得到get
TestCopy get()
{
return *this; // copy constructed. Or is it? The copy construction could
// happen at the caller. Either way it is copied and elided
}
輸出
string construct 1
copy construct 2
2 test
1 test
所以去掉冗餘副本在這一點上,我們知道有沒有必要獲取複製或分配,因爲return語句會爲我們做。這是關於OP的問題的重要部分。
但是,如果我們改變main
了一下,添加一個賦值運算符,我們可以看到一個更有趣的行爲。
int main()
{
string xstr = "test";
TestCopy x(xstr); // string constructed
TestCopy x2(""); //string constructed
x2 = x.get(); // assigned and a copy construct when returning from get
cout << "done main" << endl;
return 0;
}
輸出
string construct 1
string construct 2
copy construct 3
assigned 3 to 2
3 test
done main
2 test
1 test
從get
返回被分配到x2
,然後銷燬。 x2
被破壞,最後x
。
的問題是,這些都不'TestCopy'are的動態分配的,所以當超出範圍,他們被摧毀。 – Rodolfo
你必須明確地說,你正在函數簽名'TestCopy&get(){'注意到'''我添加了 – Rodolfo
並不完全正確,@Rodolfo。Copy elision會在返回問題時處理複製構造,並且返回本地作爲參考將在MohsenTi的面部爆炸,因爲返回的引用不再存在。 Moe on copy elision:http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization – user4581301