gpt4 book ai didi

c++ - 缺少预期的对象构造

转载 作者:太空狗 更新时间:2023-10-29 19:53:40 25 4
gpt4 key购买 nike

<分区>

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 将从该临时对象移动构造。这里发生了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com