I have code where the global resource should be configured with pretty specific code:
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 using // information that only available here doSomething(); // use the global foo object return 0; }
As you can see, foo has a global scope, but to call its constructor I need information that is available only inside main .
How can i achieve this?
The only solution I could figure out is to make foo pointer to globalClass - but this will cause the pointer to be dereferenced each time foo used. This can create a performance problem when used in a closed loop ...
PS: In the real program, main and doSomething will live in different files. And he, of course, guaranteed that foo would not be available before it was created.
Chris source share