【发布时间】:2015-02-01 12:44:13
【问题描述】:
#include <vector>
enum ListOfGameStates
{
// List of game states
};
class GameState()
{
public:
GameStates(); // Initializes protected (global) variables
virtual ListOfGameStates run() = 0;
protected:
// Heavyweigh resource managers containing all resources and other global vars
}
class GameStateManager()
{
public:
GameStateManager(); // Creates all game states
~GameStateManager(); // Deletes all game states
void run(); // Switches from one state to another state
private:
// A vector of raw pointers to game states. GameState is a base class.
std::vector<GameState*> game_states_container;
}
我想摆脱原始指针,这样我就不用担心异常和清理。是否有简单的解决方案(我是一个非常愚蠢的青少年)还是不值得?谢谢!
【问题讨论】:
-
抱歉,我很困惑,因为“push_back”不起作用。现在好了
-
一般来说,在您的问题中包含“不起作用”的内容可能是个好主意。
-
看起来你的
GameState类需要一个virtual析构函数。 -
@Galik 为什么?游戏状态没有指针,所有大对象都是全局的。其他的则是自我毁灭的智能课程。
-
GameState有一个纯虚函数,这意味着您将只使用子类,并且这些子类将从基指针中删除(通过智能指针)。没有虚拟 dtor,这是未定义的行为。
标签: c++ smart-pointers raii raw-pointer