- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个为我的项目设计的类层次结构,但我不确定如何实现它的一部分。
这是类层次结构:
class Shape { };
class Colored { // Only pure virtual functions
};
class Square : public Shape { };
class Circle : public Shape { };
class ColoredSquare : public Square, public Colored { };
class ColoredCircle : public Circle, public Colored { };
在我的部分项目中,我有一个不同类型形状的 std::vector。不过,为了运行算法,我需要将它们放在彩色对象的 std::vector 中(所有这些都是不同具体形状的派生类型,所以我需要一种方法将 Square 转换为 ColoredSquare 并将 Circle 转换为运行时的 ColoredCircle。棘手的是,“形状”类与“彩色”类位于不同的库中。完成此任务的最佳方法是什么?我考虑过进行 dynamic_cast 检查,但如果有更好的方法,我宁愿采用它。
编辑 1:
这里有一个更好的例子:
class Traceable {
public:
// All virtual functions
virtual bool intersect(const Ray& r) = 0;
// ...
};
class TraceableSphere : public Sphere, public Traceable {
};
class IO {
public:
// Reads shapes from a file, constructs new concrete shapes, and returns them to
// whatever class needs them.
std::vector<Shape*> shape_reader(std::string file_name);
};
class RayTracer {
public:
void init(const std::vector<Shape*>& shapes);
void run();
private:
std::vector<Traceable*> traceable_shapes;
};
void RayTracer::init(const std::vector<Shape*>& shapes) {
// ??? traceable_shapes <- shapes
}
void RayTracer::run() {
// Do algorithm
}
最佳答案
你可以使用装饰器模式:
class ColorDecorator public Colored
{
ColorDecorator(Shape* shape): m_shape(shape) {}
... //forward/implement whatever you want
};
如果您想将 Square 存储在 Colored vector 中,请将其包装在这样的装饰器中。
但这是否有意义值得怀疑,这取决于您的设计和备选方案。为了以防万一,还要检查访问者模式(也称为双重分派(dispatch)),您可以使用它来访问容器中的对象子集或根据对象的类型以不同方式对待它们。
关于c++ - 比 C++ 中的 dynamic_cast 更好的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569140/
我正在回答 question几分钟前,它向我提出了另一个问题: 在我的一个项目中,我做了一些网络消息解析。消息采用以下形式: [1 byte message type][2 bytes payload
有人说the use of dynamic_cast often means bad design and dynamic_cast can be replaced by virtual functi
在模板类中,我尝试使用 dynamic_cast 从文件中读取的字符串进行转换,并希望能够使用 bad_cast 异常捕获失败的转换。但是,在编译时(将测试程序设置为 double 作为模板类,我得到
在我的程序中,我有一个基类(ship)和四个派生类(pirate、mercantile、repairing, exploring) 和 repairing 的成员函数中我想知道 ship * 指向的对
本书The c++ programming language有关于 dynamic_cast 的部分,我不确定我是否理解正确。 The purpose of dynamic_cast is to de
这个问题在这里已经有了答案: How to write own dynamic_cast (4 个答案) 关闭 6 年前。 Stroustrup 书中的一个练习如下: Write a templat
我正在尝试确定 T* 指针指向的对象是否真的是 T 对象,或者其他一些不相关的类型。我试过 dynamic_cast,但它一点用处都没有,它返回指针本身而不是 null,即使很明显它没有指向有效的 T
在我的系统中,低层级对象通过调用+1 级层级对象的函数与高层级对象通信,调用+1 级层级对象的函数,等等,直到函数调用停止在配方处. 有一个Message抽象类,还有很多派生类,分别保存着不同种类的数
我已经对此进行了一些搜索,但只是为了确保: 使用dynamic_cast 将基类指针转换为派生类指针需要基类是多态的吗?不然连编译都编译不了? 谢谢。 最佳答案 您可以使用 dynamic_cast
class A { public: virtual void func() { cout(pa); pb->func1(); return 0; } 尽管 pb 指向一个
我有这段代码: void addLineRelative(LineNumber number, LineNumber relativeNumber) { list >::ite
我刚刚在这里读到这个问题 https://stackoverflow.com/a/332086/2689696它告诉动态类型转换 You can use it for more than just c
我的问题与 What's the point of IsA() in C++? 有关.我有一个性能关键代码,其中包含在特定位置处理来自派生类的特定函数,其中只有基指针可用。检查我们拥有哪个派生类的最佳
这个问题在这里已经有了答案: When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used? (12
我正在为我的游戏制作一个简单的图形引擎。 这是界面部分: class Texture { ... }; class DrawContext { virtual void set_texture
这件事让我沮丧了一个多星期。我已经浏览了该网站上有关 dynamic_casting 的各种主题,但我仍然不确定实现它的最佳方法是什么。 所以我有一个这样的基类: class baseClass {
我在尝试编译我的代码时遇到以下错误。 ERROR! ..\myCode\CPOI.cpp:68:41: error: cannot dynamic_cast 'screenType' (of type
请帮助我理解奇怪的行为: 我在处理析构函数~MyLogicObject()时使用dynamic_cast from MyObject to MyLogicObject,但编译器抛出异常:非 rtti_
我想了解装饰器模式是如何工作的,以及我可以在多大程度上“扩展”它以满足我的需要。正在关注this例如,我有扩展类 XYZ。存在派生类“KLM”(来自 XYZ) 具体来说,即使我有一个装饰器模式,派生的
为了好玩,我决定尝试制作一个简单的实体组件系统。我有一个包含所有组件的列表,我创建了一个名为 getPositionComponent 的函数,它获取实体 ID 并返回与该实体相关的位置组件。我制作的
我是一名优秀的程序员,十分优秀!