修订和总结
选项1
你可能只是声明和定义游戏对象的全局实例。 在头文件中,例如game.h:
extern Game globalGameObj;
当您在一个源文件game.h globalGameObj名称变得可见。 您还需要创建一个实际的对象。在源文件中,例如game.cc(任何类外):
Game globalGameObj;
访问它由变量名:
globalGameObj.do_some_work();
选项2
使用一个通常被称为单例模式。以下内容添加到您的游戏类(game.h):
class Game
{
public:
static Game &shared_instance() {static Game game; return game;}
private:
// Make constructor private. Only shared_instance() method will create an instance.
Game() {/*whatever initialisation you need*/}
};
你跟shared_instance()方法获得游戏实例:
Game::shared_instance().go_some_work();
你没有在上面使用任何喜欢你static class PublicInstances
。 C++允许您引入一个名称空间(例如PublicInstances)来提供名称隔离并将全局对象保留在一个地方,但它可能会是一个矫枉过正的问题。无论如何,如果你的全局对象很少,那么它很可能是一个糟糕的设计。
什么选择更好?有些人会认为应该使用单身模式。它保证只创建一个实例。然而,选项1和选项2都有相同的问题:它们在代码中引入了一个全局对象,其全部缺点归因于全局变量。我会说这个单身人士是一个伪装的全球对象。我没有看到决定技术上的原因来支持这两种选择,所以我会说这是个人品味问题。
历史注释:)
我的选项2的第一个建议是使用动态分配的博弈对象,而不是一个函数的局部静态对象。
static Game *instance() {if (!_inst) _inst = new Game(); return _inst;}
很少有人认为这不是最好的办法了,谢谢Kal,argiopeweb和Simple。 C++ 03在线程出现时会初始化静态对象。 C++ 11保证静态初始化的安全。
C++ 11条草案,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.
如果u真的想要一个全球性的,你不必须把它放在一个类中。只需将其声明在头文件中并在源文件中定义即可。但全局变量(或全局访问静态)通常是不好的设计 – Kal
我的主要问题是我通常在窗体之间旅行,我需要从一个地方获取信息到另一个地方,我如何更改全局变量或类的使用? – avatarbobo
听起来好像“TetrisGame”应该是一个命名空间,而不是一个对象。这也将消除为其定义类类型的需要。 –