Question

Shape.h

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

Rect.h

namespace Graphics {
    class Rect : public Shape {
    public:
        Rect(float x, float y);
        Rect();
        void setSize(float x, float y);
        virtual void Render(Point point);

    private:
        float sizeX;
        float sizeY;
    };
}

struct ShapePointPair {
    Shape shape;
    Point location;
};

Used like this:

std::vector<Graphics::ShapePointPair> theShapes = theSurface.getList();

for(int i = 0; i < theShapes.size(); i++) {
    theShapes[i].shape.Render(theShapes[i].location);
}

This code ends up calling Shape::Render and not Rect::Render

I'm assuming this is because it is casting the Rect to a Shape, but I don't have any idea how to stop it doing this. I'm trying to let each shape control how it is rendered by overriding the Render method.

Any ideas on how to achieve this?

Was it helpful?

Solution

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).

OTHER TIPS

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);

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.

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

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; };

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.).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top