I have a class hierarchy that I developed for my project, but I'm not sure how to implement part of it.
Here is the class hierarchy:
class Shape { };
class Colored {
};
class Square : public Shape { };
class Circle : public Shape { };
class ColoredSquare : public Square, public Colored { };
class ColoredCircle : public Circle, public Colored { };
As part of my project, I have std :: vector of various types. However, in order to run the algorithm, I need to put them in std :: vector of colored objects (all of which are derived types of various concrete shapes, so I need a method to drop a square in ColororedSquare and Circle in ColoredCircle at runtime. that the “shape” classes are in a different library than the “color” classes. What is the best method for this? I was thinking of doing a dynamic_cast check, but if there is a better way, I'd rather go with that.
Change 1:
Here is a little better Example:
class Traceable {
public:
virtual bool intersect(const Ray& r) = 0;
};
class TraceableSphere : public Sphere, public Traceable {
};
class IO {
public:
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) {
}
void RayTracer::run() {
}