比C ++中的dynamic_cast更好的解决方案 [英] Better solution than dynamic_cast in C++

查看:291
本文介绍了比C ++中的dynamic_cast更好的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类层次结构,我为一个项目设计,但我不知道如何去实现它的一部分。



这里是类层次结构:

  class Shape {}; 

class Colored {//只有纯虚函数
};

类Square:public Shape {};

类Circle:public Shape {};

class ColoredSquare:public Square,public Colored {};

类ColoredCircle:public Circle,public Colored {};

在我的项目的一部分,我有一个std :: vector不同类型的形状。为了运行一个算法,我需要把它们放在std :: vector的彩色对象(所有这些都是不同具体形状的派生类型,所以我需要一个方法来将一个Square转换为ColoredSquare和一个Circle一个ColoredCircle在运行时
棘手的事情是'形状'类是在一个不同的库比'彩色'类
什么是最好的方法来实现这个?我想过做一个dynamic_cast检查,但如果有更好的方法,我宁愿去。



编辑1:



这里有一个更好的例子:

  class Traceable {
public:
//所有虚拟函数
virtual bool intersect(const Ray& r)= 0;
// ...
};

类TraceableSphere:public Sphere,public Traceable {
};

class IO {
public:
//从文件读取形状,构造新的具体形状,并将它们返回到
//类需要它们。
std :: vector< Shape *> shape_reader(std :: string file_name);
};

类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(){
//执行算法
}

解决方案

您可以使用装饰器模式:

  class ColorDecorator public Color 
{
ColorDecorator(Shape * shape):m_shape(shape){}
... //
};

如果要在彩色矢量中存储一个正方形,请将其包装在这样的装饰器中。 p>

这是否有意义是可疑的,但这取决于你的设计和替代品。在这种情况下,还要检查访问者模式(也称为双调度),您可以使用它来访问容器中的对象子集,或根据其类型对它们进行不同的处理。


I have a class hierarchy that I designed for a project of mine, but I am not sure how to go about implement part of it.

Here is the class hierarchy:

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 { };

In part of my project, I have a std::vector of different type shapes. In order to run an algorithm though, I need to put them in a std::vector of colored objects (all of which are derived types of different concrete shapes, so I need a method to cast a Square into a ColoredSquare and a Circle into a ColoredCircle at runtime. The tricky thing is that the 'shape' classes are in a different library than the 'colored' classes. What is the best method to acomplish this? I have thought about doing a dynamic_cast check, but if there is a better way, I would rather go with that.

Edit 1:

Here's a bit better of an Example:

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
}

解决方案

You could use the decorator pattern:

class ColorDecorator public Colored
{
    ColorDecorator(Shape* shape): m_shape(shape) {}
    ... //forward/implement whatever you want
};

If you want to store a Square in a Colored vector, wrap it in such a decorator.

Whether this makes sense is questionable though, it depends on your design and the alternatives. Just in case, also check out the visitor pattern (aka double dispatch) which you could use to just visit a subset of objects in a container or treat them differently depending on their type.

这篇关于比C ++中的dynamic_cast更好的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆