我有代码必须通过相当多的代码设置全局资源:
globalClass foo;  // global variable / object; give it a memory space to live
void doSomething( void )
{
  foo.bar();      // use the global foo object
}
int main( int argc, const char *argv[] )
{
  foo( argc );   // foo can only be instantiated in main as it's using
                 // information that's only available here
  doSomething(); // use the global foo object
  return 0;
}
如您所见,foo它具有全局范围 - 但要调用它的构造函数,我需要一些仅在main.
我怎样才能做到这一点?
我能想到的唯一解决方案是创建foo一个指向globalClass- 但这会导致每次我使用foo. 在紧密循环中使用时,这可能会产生性能问题......
PS:在实际程序中main,doSomething会存在不同的文件中。当然可以保证foo在实例化之前不会被访问。