0 c++
//GUITEXT
class guitext : public entity {
public:
guitext(graphics *gfx, std::string _text, float _x, float _y,
float _size, float timeToLive);
bool update(float deltaTime, gameworld *world);
void draw(graphics *gfx);
};
void guitext::draw(graphics *gfx) { printf("draw"); }
//ENTITY
class entity {
public:
virtual bool update(float deltaTime, gameworld *world)
{ return false; }
virtual void draw(graphics *gfx) { }
};
//GAMEWORLD
void gameworld::addEntity(entity e) { entitys.push_back(e); }
//MAIN
for(int i = 0; i < (int)entitys.size(); i++) { entitys[i].draw(gfx); }
Run Code Online (Sandbox Code Playgroud)
我的gameworld课程中有一个向量.当我向这个向量添加一个guitext实体时,我希望它能调用guitext :: draw()函数.但是正在调用基类函数.我究竟做错了什么?
你做了一个矢量entity.这些对象总是有类型entity.如果要调用多态,则需要指针或引用.entity商店的矢量怎么样guitext?没有足够的空间,它不知道如何破坏它等等.