- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个指向抽象类的成员指针,我遇到了一个常见问题,即我无法复制指针指向的内容,因为我不知道指针指向哪个派生类。
在谷歌搜索解决方案时,我发现了这个问题:Copy constructor: deep copying an abstract class
它提出了这个确切的问题并给出了答案 the virtual constructor idiom ,这涉及到向抽象基类添加一个纯虚拟的 clone() 方法,然后在所有派生类中重写该方法,以便它们将分配的指针返回到它们的实际类型。
问题是这个解决方案对我不起作用,因为我需要复制的抽象类型来自库 (SFML)。这意味着我无法更改类以添加 clone() 方法。
如何在不改变抽象基类的情况下解决问题?
最佳答案
为了这个答案,我们假设您正在使用 SFML 的 sf::Shape
抽象类:
所以,您可能正在处理 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/
我对 java 中的抽象有点困惑。 我查了很多页面说抽象就是数据隐藏(隐藏实现)。 我对抽象的理解是它是“部分实现”。只需在抽象类/接口(interface)中定义您需要的内容,然后扩展/实现它们并添
我是 Ruby 的新手,主要来自 C# 和 ActionScript 3(以及其他语言)。我对抽象功能很好奇。具体来说,包装和抽象 Ruby 的 FTP 和 SFTP 库。 我四处搜索,发现了一个名为
目录 Java基础知识(抽象) 抽象 抽象定义 abstract的使用 定义抽象类
这个月我花了一些时间与 Emacs Lisp 进行斗争,试图获得更好地满足我需求的自动缩进。令人惊讶的是,大多数缩进代码是多么低级。我只看到了很少的抽象,例如 搜索不在字符串或注释中的第一个正则表达式
我有以下内容: public abstract class Foo{ //contents of Foo // ... public class Bar extends
我有三个类(class)(A 类、B 类和 C 类)。 类 A 调用 B 的实例并运行 start()。 B类扩展了Thread,因此当调用start()时,run()方法中的任何内容都会被执行。 在
这个问题已经有答案了: Calling a subclass method from superclass (5 个回答) 已关闭 7 年前。 Klasse1是一个抽象类,有一个 foo()方法。 K
我有一个这样的函数: def test(): x = "3" # In actual code, this is computed if x is None: retu
我有两个基类之间的关系: public abstract class RecruiterBase { // Properties declare here // Constructors de
这是我第一次发帖,但我遇到了很多问题。我目前有一个带有标题的 AbstractDevice 类: public abstract class AbstractDevice> implements De
我有一个 MotorDefinition 类和一个名为 Motor 的抽象类: class MotorDefinition { public: MotorDefinition(int p1,
是否有任何方法可以在这种代码(sass)中制定 css 的抽象规则: #cottage-image-gallery input:nth-of-type(1):checked ~ label:nth-o
是否可以声明一个已知的基类型并允许传输所有派生类型? [ServiceContract] public interface IService { [OperationContract]
我目前正在为基于 Java 的文本游戏开发角色生成机制,但我遇到了一个问题,看不出哪里出了问题。我有一个“Character”类,它是抽象的,然后是另一个类“NPCharacter”,它是建立在这个之
抱歉,标题令人困惑。不太确定如何表达它,这可能是问题所在! 我正在寻找一个好的抽象来用于涉及并发线程的情况。 我已经接近了,但还不是很清楚。 稍微简化一下,我在 Android 手机上收集了两种传感器
提前感谢您阅读本文。我不完全理解如何/何时使用摘要,所以我试图在我从事的每个项目中考虑它,看看它是否会在某一天全部点击 Smile | :) 此外,可访问性级别(私有(private)、 protec
我正在探索用于生成 Web 内容的 XML -> XSLT -> HTML 模因。我的 XSLT 经验很少。 我很好奇 XSLT 中有哪些机制可用于处理抽象或“重构”。 例如,使用通用 HTML 和服
在这些谈话中 Nicholas Zakas和 Addy Osmani他们讨论了在构建大型 Javascript 应用程序时将外观模式用作沙箱的想法,以便将应用程序与底层基础库分离。 这种解耦理论上允许
我使用C++和CUDA/C,想为特定问题编写代码,但遇到了一个非常棘手的简化问题。 我在并行编程方面的经验不容忽视,但相当有限,我无法完全预见到此问题的特殊性。 我怀疑是否有一种方便甚至“轻松”的方式
假设我有: trait A class B extends A class C extends A 有没有办法配置类型参数: class Foo[AType <: A with canCreateIn
我是一名优秀的程序员,十分优秀!