好的,首先,我试图将类实例从范围移到范围之外。我不确定我是否使用了正确的条款,但这里是:
Button Btn; //Declare blank object out of scope..
LRESULT __stdcall WindowProcedure(/*Params here*/)
{
switch(message)
{
case WM_CREATE:
{
Btn = std::move(Button("Title", Point(0, 0), 95, 22, hwnd)); //Move assign the scoped object..
}
break;
case WM_COMMAND:
Btn.SetText("New Title"); //Access the Non-Scoped button to see if the Move really worked.
break;
}
return 0;
}
如上所示,我尝试通过移动赋值将范围对象移动到范围之外。所以我希望范围对象将其内容分配给我的非范围对象,然后销毁范围之一。
然而,当收到WM_COMMAND时,它会抛出一个错误,所以我知道有些事情是错的。我似乎无法看到有什么问题。在我的Bitmap类中执行相同/类似的技术(没有继承),它可以工作..但是对于继承,我似乎搞砸了一些如何。
我的代码如下:
class Control
{
private:
HMENU ID;
HWND Handle, Parent;
std::string Class, Title;
void Swap(Control &C);
public:
Control(const Control &C) = delete; //Copying is not allowed.
Control(Control &&C); //Moving is allowed.
Control(DWORD dwExStyle, /*Other params here*/);
Control(DWORD dwExStyle, /*More params here*/);
virtual ~Control();
virtual void Dispose();
Control& operator = (const Control &C) = delete; //Copying through assignment not allowed.
Control& operator = (Control&& C); //Move through assignment allowed.
protected:
Control();
bool Initialized;
static LRESULT __stdcall SubClass(HWND Window, UINT Msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
};
class Button : public Control
{
public:
Button();
Button(const Button &B); //Copying is allowed.
Button(Button&& B); //Moving is allowed.
Button(std::string Title, Point Location, /*Other params here*/);
Button(std::string Title, DWORD dwStyle, /*More params here*/);
virtual ~Button();
virtual void Dispose() override;
Button& operator = (const Button &B) = delete; //Copy through assignment not allowed. (cannot be overriden)
Button& operator = (Button&& B); //Move through assignment allowed. Non-virtual (cannot be overriden)
};
Control::~Control() {}
void Control::Swap(Control &C)
{
using std::swap;
swap(ID, C.ID);
swap(Handle, C.Handle);
swap(Parent, C.Parent);
//Swap all members..
}
/*All other constructors here..*/
Control::Control(Control &&C) : ID(std::move(C.ID)), Handle(std::move(C.Handle)), /*move all member*/ {}
void Control::Dispose()
{
ID = nullptr;
Parent = nullptr;
if (Handle != nullptr)
{
DestroyWindow(Handle);
Handle = nullptr;
}
}
Control& Control::operator = (Control&& C)
{
if (this->Handle != C.Handle)
{
/*this->ID = std::move(C.ID);
this->Handle = std::move(C.Handle);
this->Parent = std::move(C.Parent);
this->Class = std::move(C.Class);
this->Title = std::move(C.Title);
this->dwExStyle = std::move(C.dwExStyle);
this->dwStyle = std::move(C.dwStyle);
this->Location = std::move(C.Location);
this->Width = std::move(C.Width);
this->Height = std::move(C.Height);*/
C.Swap(*this); //Do I use my swap func? Or do I use std::move?
C.Dispose();
}
return *this;
}
//Button constructors are the same as the Control constructors with the initialization stuff.. just different parameters.
Button::Button(const Button &B) : Control(B.ID + 1, B.Class, B.Title, /*all other params */) {} //Copy constructor..
Button& Button::operator = (Button&& B)
{
Control::operator = (std::move(B)); //I believe it is this line that probably throws.
return *this;
}
答案 0 :(得分:1)
你移动控制内容,交换回来,然后将它和按钮一起处理。
这不仅无法正确移动控件,还会使其他按钮无效。
编写一个工作移动构造函数。使用移动交换习惯用于分配移动。如果移动ctor和swap都是正确的,那么这将有效。