views:

530

answers:

7

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?

+1  A: 

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

Locksfree
+13  A: 

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

Dave Hinton
auto_ptr will loose data as the ShapePointPair is copied around in the vector
Mark
@Mark: noted, ta.
Dave Hinton
Thanks. I'd been hoping to do it without pointers since the shapes don't use much memory so are simple to copy around on the stack, but it seems thats not possible. Thanks all for the quick replies.
Simie
+3  A: 

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);
Georg Fritzsche
Thanks. I hadn't heard about slicing before, will keep this in mind next time.
Simie
+2  A: 

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.

moonshadow
+1  A: 

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

Mark
A: 

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

Peter Mortensen