C++ Overridden method not getting called

后端 未结 8 1345
逝去的感伤
逝去的感伤 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.

    0 讨论(0)
  • 2021-02-13 12:00

    You could try boost::ptr_vector

    http://www.boost.org/doc/libs/1_40_0/libs/ptr_container/doc/ptr_container.html

    0 讨论(0)
提交回复
热议问题