我得到了经典的Shape层次结构示例......
struct Shape { // abstract type
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;
};
Run Code Online (Sandbox Code Playgroud)
一个Shapes容器,填充矩形和圆形
std::list<Shape*> container;
Run Code Online (Sandbox Code Playgroud)
和打印功能(在我的情况下,那些是碰撞检测功能)
void print_types (Shape&, Shape&) {
std::cout << "Shape, Shape" << std::endl;
}
void print_types (Rectangle&, Rectangle&) {
std::cout << "Rectangle, Rectangle" << …Run Code Online (Sandbox Code Playgroud)