0

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

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

1 回答 1

1

您移动控件的内容,交换回来,然后同时处理它和按钮。

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

编写一个有效的移动构造函数。使用移动交换成语进行分配移动。如果 move ctor 和 swap 都写对了,这将起作用。

于 2013-03-09T19:55:52.410 回答