C++ Overridden method not getting called

后端 未结 8 1362
逝去的感伤
逝去的感伤 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:57

    The problem is that in your vector you are storing copies of Shape objects, and copying a Shape object does not copy the data or functionality of its derived classes - you're slicing the polymorphism away.

    Manage the objects using new and delete, and arrange for your vector to store pointers to them.

提交回复
热议问题