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

时间:2016-12-13 14:51:12

标签: c++ wxwidgets

我想为我的类实现移动赋值运算符,它也将移动我的所有动态绑定。我工作得很好,直到我尝试从wxEvtHandler继承。我玩了很多时间,并且当它与wxEvtHandler的继承一起工作时也无法找到解决方案。请帮忙。

UI类:

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

主窗口类:

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

1 个答案:

答案 0 :(得分:0)

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