Repetition and generalization
Option 1
You can simply declare and define a global instance of the Game object. In the header file, for example. game.h:
extern Game globalGameObj;
game.h , globalGameObj .
. , . game.cc( ):
Game globalGameObj;
:
globalGameObj.do_some_work();
2
, singleton. Game (game.h) :
class Game
{
public:
static Game &shared_instance() {static Game game; return game;}
private:
Game() {}
};
shared_instance():
Game::shared_instance().go_some_work();
, static class PublicInstances. ++ (, PublicInstances), , , , . , , , , .
? , . , . 1 2 : , . , singleton - , . , , .
:)
2 , Game, .
static Game *instance() {if (!_inst) _inst = new Game(); return _inst;}
, , Kal, argiopeweb Simple. ++ 03 . ++ 11 .
++ 11 draft, secion 6.7
such a variable is initialized the first time control passes through its declaration;
such a variable is considered initialized upon the completion of its initialization. [...]
If control enters the declaration concurrently while the variable is being initialized,
the concurrent execution shall wait for completion of the initialization.