【问题标题】:Appending to polymorphic list in C++?附加到 C++ 中的多态列表?
【发布时间】:2019-04-28 21:58:42
【问题描述】:

我是 C++ 新手,并试图使列表多态/接受从基类派生的任何内容。问题是这个列表必须是私有的,使用单独的方法来附加和查询它。

经过一番研究,我能够通过智能指针以安全的方式接近。

这是我得到的结果:

class Shape
{
public:
    Shape(std::string name)
    {
        this->name = name;
    }

    std::string name;
    std::string getName(void)
    {
        return this->name;
    }
};

class ShapeCollector
{
public:
    void addShape(Shape shape)
    {
        this->shapes.push_back(std::make_unique<Shape>("hey"));
    }

private:
    std::vector <std::unique_ptr<Shape>> shapes;
};

我希望能够将 make_unique 调用替换为 shape 参数,但是我尝试的任何操作似乎都无法正常播放。

我可以在 ShapeCollector 中创建每个派生类,将构造函数参数镜像为参数,但这感觉很不直观。

任何帮助将不胜感激!

【问题讨论】:

  • 您的代码应该可以工作。您面临的具体问题是什么?请在此处按要求发布minimal reproducible example 重现您的问题。
  • make_unique 不会以多态方式工作,因为 make_unique 使用的构造函数的选择是在编译时完成的。这不是确切的骗局,但此答案中提出的解决方案可以很容易地适应您的问题:stackoverflow.com/a/55879235/3723423
  • @πάνταῥεῖ OP 解释说他/她不能让它以多态方式运行(参见代码 sn-p 后面的两句话)。
  • 如果您要传递要添加的多态类型的对象,那么您需要将它们作为unique_ptrs 传递,或者将虚拟clone() 方法添加到基类。附带说明一下,您的 Shape 不是多态的,即使是多态的,您也无法通过值传递它。
  • 感谢大家的回复。 @πάνταῥεῖ 此代码有效,但我希望能够从参数 shape 添加。 @Christophe谢谢,我会看看这个答案。 @bipll 是的,这是我之前尝试实现的,但我找不到此 addShape 方法的匹配参数类型。形状目前不是多态的,是的。它将是将其他对象添加到此列表的基类。我省略了它们以使示例代码更简洁。

标签: c++ list class polymorphism derived


【解决方案1】:

addShape将派生类作为模板参数:

template<class Derived, class... Args>
void addShape(Args&&... args) {
    // std::forward will correctly choose when to copy or move
    std::unique_ptr<Shape> shape (new Derived(std::forward<Args>(args)...));
    shapes.push_back(std::move(shape)); 
}

这将允许您为派生类s constructor. For example, if we have aCircle` 类提供addShape 的参数:

class Circle : public Shape {
    double radius;
    double x;
    double y;
   public:
    Circle(double radius, double x, double y) 
      : Shape("circle"), radius(radius), x(x), y(y) 
    {}
};

添加很简单:

ShapeCollector shapes;
shapes.addShape<Circle>(10.0, 0.0, 0.0); 

【讨论】:

  • 正是我想要实现的目标,非常感谢!
  • 太棒了!顺便说一句,我做了一个小修正(addShape 是添加一个圆圈而不是派生的)
  • 没问题,我自己做了这个改变。忘了包括它!
猜你喜欢
  • 2017-10-20
  • 2021-10-06
  • 2021-03-21
  • 2021-07-12
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
相关资源
最近更新 更多