Shape.hC++重写方法没有得到所谓的
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;
};
像这样来使用:
std::vector<Graphics::ShapePointPair> theShapes = theSurface.getList();
for(int i = 0; i < theShapes.size(); i++) {
theShapes[i].shape.Render(theShapes[i].location);
}
该代码最终调用形状::渲染和不是矩形::渲染
我假设这是因为它将Rect塑造成Shape,但我不知道如何阻止它这样做。我试图让每个形状通过重写Render方法来控制它的渲染方式。
有关如何实现此目的的任何想法?
也许你想向我们展示的是创建矢量元素的代码? – 2009-09-18 11:45:56
问题和解决方案几乎与此问题相同http://stackoverflow.com/questions/1230006/问题。在那里,你有一个(struct包含一个)具体基类的向量,如果你期望Rect :: Render被调用,那么你必须通过* slicing *派生类来创建它。 – 2009-09-18 11:53:27