I got a classic example of a form hierarchy ...
struct Shape {
Shape (int x, int y);
int x;
int y;
};
struct Rectangle : public Shape {
Rectangle (int x, int y, int w, int h);
int w;
int h;
};
struct Circle : public Shape {
Circle (int x, int y, int r);
int r;
};
Shapes container filled with rectangles and circles
std::list<Shape*> container;
and print functions (in my case, these are collision detection functions)
void print_types (Shape&, Shape&) {
std::cout << "Shape, Shape" << std::endl;
}
void print_types (Rectangle&, Rectangle&) {
std::cout << "Rectangle, Rectangle" << std::endl;
}
void print_types (Rectangle&, Circle&) {
...
Of course, when I do this:
std::list<Shape*> it;
Rectangle r (0, 0, 32, 32);
for (it = container.begin(); it != container.end(); it++)
print_types(r, **it);
I do not want to print only the lines "Shape, Shape". I know virtual methods, dynamic_casts and visitors. But is there any elegant way to get out of this without these solutions and preserve my external functions?