class A
{
public:
A()
{
wcout << L"Empty constructed." << endl;
}
A (LPCWSTR Name)
: m_Name(Name)
{
wcout << L"Constructed." << endl;
}
friend void swap (A& Lhs, A& Rhs)
{
using std::swap;
swap(Lhs.m_Name, Rhs.m_Name);
}
A (A&& Other)
{
wcout << L"Move constructed." << endl;
swap(*this, Other);
}
A (const A& Other)
: m_Name(Other.m_Name)
{
wcout << L"Copy constructed." << endl;
}
A& operator= (A Other)
{
wcout << L"Assignment." << endl;
swap(*this, Other);
return *this;
}
~A()
{
wcout << L"Destroyed: " << m_Name.GetString() << endl;
}
private:
CString m_Name;
};
int
wmain()
{
A a;
a = A(L"Name"); // Where is the construction of this temp object?
return 0;
}
這是輸出我得到了上面的代碼:預期目標建設缺少
Empty constructed.
Constructed.
Assignment.
Destroyed:
Destroyed: Name
見有註釋的行。我期望的是在那裏構造一個臨時對象,並且運算符中的參數Other將從該臨時對象獲得移動構造。這裏發生了什麼事?
我的評論關於在標準中的位置是否指定複製elision可能會以可見的副作用進行復制問題中的答案。 – TripShock 2013-08-12 18:04:43