C++ 保存合适的矢量/地图的继承类的所有实例

问题描述 投票:0回答:0

使用 CWindow 类,我将其他类的各个对象存储在 m_Db 映射中。 CWindow中的add方法必须这样使用。我试图在保存方法上使用多态性,以便“正确”保存每个对象,但不知何故我做不到。

class CWindow
{
  public:

    CWindow* add(CWindow &x);
    virtual void save(CWindow &x);

    ...

  protected:
    int m_Id;
    string m_Title;
    CRect m_AbsPos;
    static multimap<int,unique_ptr<CWindow>> m_Db; 

};

CWindow *CWindow::add(CWindow &x)
{
  save(x);
  return this;
}

void CWindow::save(CWindow &x)
{
  unique_ptr<CWindow> ptr = make_unique<CWindow>(move(x)); 
  m_Db.emplace(ptr->m_Id, move(ptr));
}

class CButton : public CWindow
{
    virtual void save(CWindow &x) override;
    ...
};


void CButton::save(CWindow &x)
{ 
  // I don't know how to edit the code here for proper functionality
  m_Db.emplace(1, make_unique<CButton>(dynamic_cast<CButton>(x*)));
}

保存合适的矢量/地图的继承类的所有实例

c++ inheritance polymorphism
© www.soinside.com 2019 - 2024. All rights reserved.