使用动态绑定移动 class 的赋值运算符

Move assignment operator for class with dynamic bindings

我想为我的 class 实现移动赋值运算符,这也将移动我所有的动态绑定。我工作得很好,直到我尝试从 wxEvtHandler 继承。我玩了很多时间,但找不到解决方案,因为它也可以很好地从 wxEvtHandler 继承。请帮忙。

UI class:

class FrameUi : public wxFrame
{
public:
    FrameUi(wxWindow* parent)
        : wxFrame(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize)
    {
        m_panel = new wxPanel(this);

        wxBoxSizer* mainS = new wxBoxSizer(wxVERTICAL);
        m_button = new wxButton(m_panel, wxID_ANY);
        mainS->Add(m_button, 0, wxALL);

        m_panel->SetSizer(mainS);

        wxBoxSizer* m_panelSizer = new wxBoxSizer(wxVERTICAL);
        m_panelSizer->Add(m_panel, 1, wxEXPAND, 5);

        SetSizer(m_panelSizer);
    }

    wxPanel* m_panel;
    wxButton* m_button;
};

控制器class:

class Frame
    : public wxEvtHandler // Without inheriting from it, binding work on new object
{
public:
    Frame()
        : m_ui(0)
    { }

    Frame(wxWindow* parent)
        : m_ui(new FrameUi(parent))
    { 
        m_ui->m_panel->Bind(wxEVT_LEFT_DOWN, &Frame::onLeftDown, this);
        m_ui->m_button->Bind(wxEVT_BUTTON, &Frame::onButton, this);
    }

    Frame(const Frame&) = delete;
    Frame& operator=(const Frame&) = delete;

    Frame& operator=(Frame&& rhs)
    {
        if (&rhs != this)
        {
            m_ui = rhs.m_ui;
            rhs.m_ui = nullptr;
        }
        return *this;
    }

    void onLeftDown(wxMouseEvent& event) { wxLogDebug("Left Down"); }

    void onButton(wxCommandEvent&) { wxLogDebug("Pressed"); }

    FrameUi* m_ui;
};

主要windowclass:

class MainWnd : public wxFrame
{
public:
    MainWnd()
        : wxFrame(NULL, wxID_ANY, wxEmptyString)
    {
        wxBoxSizer* s = new wxBoxSizer(wxVERTICAL);
        SetSizer(s);
    }

    Frame frame;
};

申请入口点:

class WxguiApp
    : public wxApp
{
public:
    bool OnInit() override
    {
        MainWnd* mainWnd = new MainWnd();
        mainWnd->Show();
        SetTopWindow(mainWnd);

        mainWnd->frame = Frame(mainWnd); // If comment inheritance from wxEvtHandler, binding will work well
        mainWnd->frame.m_ui->Show();

        return true;
    }
};

IMPLEMENT_APP(WxguiApp);

wxEvtHandler 不可移动,因此您不能使从它派生的 class 可移动。最简单的解决方案当然就是不继承它。