gpt4 book ai didi

c++ - 使用继承 move 分配/构造函数会抛出范围外的错误

转载 作者:行者123 更新时间:2023-11-28 07:43:38 26 4
gpt4 key购买 nike

好的,首先,我尝试将一个类实例从范围内移到范围外。我不确定我是否使用了正确的术语,但这里是:

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;
}

因此,如上所示,我试图通过 move 赋值将作用域对象移到作用域之外。所以我希望作用域对象将其内容分配给我的非作用域对象,然后销毁作用域对象。

但是,当收到 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;
}

最佳答案

你把控件的内容移过来,换回来,然后把它和按钮都处理掉。

这不仅无法正确 move 控件,还会使其他按钮无效。

编写一个有效的 move 构造函数。使用 move 交换成语来分配 move 。如果 move ctor 和 swap 都写对了,这就可以了。

关于c++ - 使用继承 move 分配/构造函数会抛出范围外的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15314936/

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