gpt4 book ai didi

c++ - 使用库中的抽象类时如何解决 "deep copy of an abstract class"问题?

转载 作者:行者123 更新时间:2023-12-04 15:08:51 26 4
gpt4 key购买 nike

我有一个指向抽象类的成员指针,我遇到了一个常见问题,即我无法复制指针指向的内容,因为我不知道指针指向哪个派生类。

在谷歌搜索解决方案时,我发现了这个问题:Copy constructor: deep copying an abstract class

它提出了这个确切的问题并给出了答案 the virtual constructor idiom ,这涉及到向抽象基类添加一个纯虚拟的 clone() 方法,然后在所有派生类中重写该方法,以便它们将分配的指针返回到它们的实际类型。

问题是这个解决方案对我不起作用,因为我需要复制的抽象类型来自库 (SFML)。这意味着我无法更改类以添加 clone() 方法。

如何在不改变抽象基类的情况下解决问题?

最佳答案

为了这个答案,我们假设您正在使用 SFML 的 sf::Shape 抽象类:

sf::Shape hierarchy

所以,您可能正在处理 sf::CircleShape , sf::ConvexShape , 和 sf::RectangleShape通过 sf::Shape 的对象指针或引用。这些是具体类。

而不是通过 sf::Shape 工作您可以定义以下抽象类 ClonableShape :

struct ClonableShape {
virtual ~ClonableShape() = default;

virtual std::unique_ptr<ClonableShape> clone() const = 0;

// extend at will with the member functions from sf::Shape, e.g.:
virtual const sf::Vector2f& getPosition() const = 0;
// ...
};

您可以使用此接口(interface)以多态方式克隆这些对象,方法是使用上面的具体类实例化以下类模板:

template<typename T>
struct Clonable: T, ClonableShape {
Clonable() = default;

template<typename... Args>
Clonable(Args... args): T(args...) {}

std::unique_ptr<ClonableShape> clone() const override {
return std::make_unique<Clonable<T>>(*this);
}

const sf::Vector2f& getPosition() const override {
// careful not to call ClonableShape::getPosition() instead
return T::getPosition();
}
};

也就是说,此解决方案依赖于继承,但您可能需要考虑组合 而不是X

最后,您可以多态地克隆形状对象:

std::unique_ptr<ClonableShape> shapeA(new Clonable<sf::RectangleShape>(sf::Vector2f{4, 4}));
std::unique_ptr<ClonableShape> shapeB = std::make_unique<Clonable<sf::CircleShape>>(4.f);

std::unique_ptr<ClonableShape> shapeC = shapeA->clone();
auto shapeD = shapeB->clone();

sf::Shape公开源自 sf::Drawable 并且 SFML 库中有几个函数接受对 sf::Drawable 的引用,例如 sf::RenderWindow::draw() ,您可能想要扩展 ClonableShape上面的接口(interface)能够隐式转换为 sf::Drawable&通过添加:

virtual operator sf::Drawable&() = 0;

然后在 Clonable<> 中覆盖它作为:

operator sf::Drawable&() override { return *this; }

这样,如果你有一个像这样的函数:

void draw(sf::Drawable&);

您只需编写即可调用它:

draw(*shapeA);

X 如果不太依赖具体类——例如,如果您不直接使用 Clonable<sf::RectangleShape>Clonable<sf::CircleShape> – 那么您可能希望从继承切换到组合,因为您不会过多地依赖于通过将具体形状作为公共(public)基础而继承的成员函数,而是依赖于那些您在 ClonableShape 中指定:

template<typename T>
class Clonable: public ClonableShape {
T shape_; // as data member instead of public base
public:
Clonable() = default;

template<typename... Args>
Clonable(Args... args): shape_(args...) {}

std::unique_ptr<ClonableShape> clone() const override {
return std::make_unique<Clonable<T>>(*this);
}

const sf::Vector2f& getPosition() const override {
// careful not to call ClonableShape::getPosition() instead
return shape_.getPosition();
}

// operator sf::Drawable&() override { return shape_; }
};

关于c++ - 使用库中的抽象类时如何解决 "deep copy of an abstract class"问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65587698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com