从基类C++; 我是C++新手,我很难找出我的虚拟函数有什么问题。所以,这里是我所拥有的: 绅士风度.h class GEntity { public: //... virtual void tick(void); virtual void render(void); //... }; class GLiving : public GEntity { public: //... virtual void tick(void); virtual void render(void); //... }; class Player : public GLiving { public: //... void tick(void); void render(void); //... };

从基类C++; 我是C++新手,我很难找出我的虚拟函数有什么问题。所以,这里是我所拥有的: 绅士风度.h class GEntity { public: //... virtual void tick(void); virtual void render(void); //... }; class GLiving : public GEntity { public: //... virtual void tick(void); virtual void render(void); //... }; class Player : public GLiving { public: //... void tick(void); void render(void); //... };,c++,function,virtual,C++,Function,Virtual,绅士风度.cpp //... void GEntity::tick(void){} void GEntity::render(void){} //... //... void GEntity::tick(void){} void GEntity::render(void){} //... //... void GEntity::tick(void) { //Here there's some actual code that updates the player } void GEn

绅士风度.cpp

//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
//...
void GEntity::tick(void)
{
    //Here there's some actual code that updates the player
}
void GEntity::render(void)
{
    //Here there's some actual code that renders the player
}
//...
GLiving.h

class GEntity
{
public:
    //...
    virtual void tick(void);
    virtual void render(void);
    //...
};
class GLiving : public GEntity
{
public:
    //...
    virtual void tick(void);
    virtual void render(void);
    //...
};
class Player : public GLiving
{
public:
    //...
    void tick(void);
    void render(void);
    //...
};
GLiving.cpp

//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
//...
void GEntity::tick(void)
{
    //Here there's some actual code that updates the player
}
void GEntity::render(void)
{
    //Here there's some actual code that renders the player
}
//...
然后,我还有从GLiving(玩家,敌人)派生的其他类,它们实现了这两种方法的各自版本: Player.h

class GEntity
{
public:
    //...
    virtual void tick(void);
    virtual void render(void);
    //...
};
class GLiving : public GEntity
{
public:
    //...
    virtual void tick(void);
    virtual void render(void);
    //...
};
class Player : public GLiving
{
public:
    //...
    void tick(void);
    void render(void);
    //...
};
Player.cpp

//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
//...
void GEntity::tick(void)
{
    //Here there's some actual code that updates the player
}
void GEntity::render(void)
{
    //Here there's some actual code that renders the player
}
//...
现在,如果我声明一个类Player的对象,并调用render/tick方法,一切都很顺利,但我的情况是,我将我的Player添加到GEntity的arraylist(我创建的一个结构)中,然后,当我得到它时,我得到它作为GEntity,我需要调用render/tick方法,而不知道它的派生类。。。 我已经尝试过上面的代码,但是在我调用render或tick方法的那一行中,在提取的GEntity…
…我想实现的目标是可能的吗?

(很抱歉,如果我的英语不是很好,但我是意大利人)

如果你有一个
优雅的数组
,那么每次你“添加”一个派生类型时,都会发生类似的情况:

GEntity g;
Player p;
g = p; // object slicing, you assigned a Player to a GEntity object.
g.render(); // GEntity::render() gets called
另一方面,可以使用指向基类的指针访问派生方法:

GEntity* g;
Player p;
g = &p;
g->render(); // calls Player::render()
因此,处理容器中多态性的一种方法是使用指向基类的(最好是智能的)指针的数组/容器。为了简单起见,本例使用原始指针,但您应该在实际代码中使用:

std::vector<CEntity*> entities;
entities.push_back(new Player);
entities.push_back(new GLiving);

// some c++11
for ( auto e : entities) {
  e->render();
}
std::向量实体;
实体。推回(新玩家);
实体。推回(新滑动);
//一些c++11
用于(自动e:实体){
e->render();
}

问题可能存在于
arraylist
中。它是否包含指向
GEntity
的指针或
GEntity
的实际实例?数组/结构声明如何?它包含实际的实例它看起来像你正在经历的。你需要的是指针,而不是实例。使用智能指针作为首选项。是否应该为(\u auto\e:entities)编写