C++ Overridden method not getting called

后端 未结 8 1347
逝去的感伤
逝去的感伤 2021-02-13 11:20

Shape.h

namespace Graphics {
    class Shape {
    public:
        virtual void Render(Point point) {};
    };
}

Rect.h

namespa         


        
8条回答
  •  礼貌的吻别
    2021-02-13 11:51

    Here's your problem:

    struct ShapePointPair {
            Shape shape;
            Point location;
    };
    

    You are storing a Shape. You should be storing a Shape *, or a shared_ptr or something. But not a Shape; C++ is not Java.

    When you assign a Rect to the Shape, only the Shape part is being copied (this is object slicing).

提交回复
热议问题