C++ Overridden method not getting called

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

    You are accessing the shape object directly for the override to work you need to access the object via a pointer or references.

    For example when you assigne the Shape into the ShapePointPair the code will 'slice' the object and only copy the Shape bit into the ShapePointPair

    Doing this will mean you have to watch memory management - so you could use a smart pointer in the struct ShapePointPair { smart_pointer shape; Point location; };

提交回复
热议问题