Brandon
Brandon

Reputation: 23475

Move Assignment/Constructor with Inheritance throws error out of scope

Ok so first, I'm trying to move a class instance from in scope to out of scope. I'm not sure if I'm using the right terms or not but here goes:

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

So as shown above, I attempted to Move the scoped object to out of scope through move assignment.. So I'm expecting the scoped object to have assigned its contents to my non-scoped object and then destroy the scoped one.

However, when WM_COMMAND is received, it will throw an error so I know something has to be wrong. I cannot seem to see what is wrong though. Doing the same/similar techniques in my Bitmap class (has no inheritance), it works.. But with inheritance, I seem to mess it up some how.

My code is as follows:

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

Upvotes: 0

Views: 165

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275650

You move the contents of control over, swap back, then dispose both it and the button.

Not only does this fail to move a Control properly, it also invalidates the other Button.

Write a working move constructor. Use move swap idiom for assign move. If both move ctor and swap is written right this will work.

Upvotes: 1

Related Questions