我是C++的新手,我很難弄清楚我的虛擬函數有什麼問題。所以,這裏是我有:
GEntity.h從基類C++調用虛擬方法
class GEntity
{
public:
//...
virtual void tick(void);
virtual void render(void);
//...
};
GEntity.cpp
//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
GLiving.h
class GLiving : public GEntity
{
public:
//...
virtual void tick(void);
virtual void render(void);
//...
};
GLiving.cpp
//...
void GEntity::tick(void){}
void GEntity::render(void){}
//...
然後,我有從GLiving(玩家,敵人),它們實現了自己的這兩種方法的版本獲得其他類: Player.h
class Player : public GLiving
{
public:
//...
void tick(void);
void render(void);
//...
};
Player.cpp
//...
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方法,一切都很順利,但是我處於一種情況,我將我的播放器添加到GEntity的ArrayList(一個我創建的結構體),然後,當我將它返回時,我得到它作爲一個GEntity,我需要調用渲染/滴答方法,而不知道它的派生類... 我已經嘗試使用上面的代碼,但我得到一個訪問衝突行中,我打電話或者渲染或刻度方法,對提取的GEntity ...
...是我想甚至可以實現的目標?
(抱歉,如果我的英語不太好,但我是意大利)
這個問題可能存在於'arraylist'中。它是否包含指向「GEntity」或「GEntity」實際實例的指針? – Yakk
數組/結構聲明怎麼樣?這是關鍵。 – Ajay
它包含實際實例 – Sylar