C++ Overridden method not getting called

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

    The polymorphism will only work from a pointer to a shape, not from a shape object.

    0 讨论(0)
  • 2021-02-13 11:45

    No, it is not casting.

    You can instead store a reference to baseclass Point:

    struct ShapePointPair {
            Shape shape;
            Point &location;
    };
    

    This reference must be set at construction time for struct ShapePointPair. Add a constructor to ShapePointPair for this purpose. It must be passed (newly created) instances of Rect.

    Also observe the memory management responsiblities (proper written destructors, etc.).

    0 讨论(0)
  • 2021-02-13 11:46

    I'm not sure to explain well because of my english is poor.

    I think you should have to use it reference or pointer type. because shape is exactly defined what it has to do.

    If you use your code directly, your child try to copy and do shape's working. That is why doesn't work your override function.

    use pointer or reference like this.

    pointer.h

    class Parent {
    public:
        virtual void work() { printf("parent is working now\n"); }
    };
    class Child1 {
    public:
        virtual void work() { printf("child1 is working now\n"); }
    };
    class Child2 {
    public:
        virtual void work() { printf("child2 is working now\n"); }
    };
    struct Holder {
        Parent* obj1;
        Parent* obj2;
    };
    int main() {
        Child1 child1;
        Child2 child2;
        Holder holder = { &child1, &child2 };
        holder.obj1->work();
        holder.obj2->work();
        return 0;
    }
    

    reference.h

    class Parent {
    public:
        virtual void work() { printf("parent is working now\n"); }
    };
    class Child1 {
    public:
        virtual void work() { printf("child1 is working now\n"); }
    };
    class Child2 {
    public:
        virtual void work() { printf("child2 is working now\n"); }
    };
    struct Holder {
        Parent& obj1;
        Parent& obj2;
    };
    int main() {
        Child1 child1;
        Child2 child2;
        Holder holder = { child1, child2 };
        holder.obj1.work();
        holder.obj2.work();
        return 0;
    }
    

    *ps: personally i use abstract function(virtual void something() = 0;). because i also forgot about it sometimes so i catch it as syntax error.

    0 讨论(0)
  • 2021-02-13 11:50

    This problem is called slicing - you lose the derived functionality when copying to a base. To avoid this use pointers to the base class, i.e.

    std::vector<Graphics::Shape*> s;
    s.push_back(&some_rect);
    
    0 讨论(0)
  • 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<Shape> 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).

    0 讨论(0)
  • 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; };

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