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
请参阅带有注释的行。我期望在那里构造一个临时对象,并且 operator= 中的参数 Other 将从该临时对象中移动构造。这里发生了什么事?