小编Sim*_*mon的帖子

c ++:没有强制转换的包含对象的确切类型

我得到了经典的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)

c++ oop polymorphism inheritance covariance

3
推荐指数
1
解决办法
185
查看次数

标签 统计

c++ ×1

covariance ×1

inheritance ×1

oop ×1

polymorphism ×1