Question: I am having a hard time debugging a code which contains a variable that holds an object. Its value doesn't retain. Like this,

```
//foo.cpp
CCustomClass cfoo = CStaticClass::cfoo_static; // cfoo_static is a static member object

void footest()
{
    // code here that use cfoo but cfoo is empty
}

//main.cpp
int main()
{
   footest();
   return 0;
}

```

Please take note that cfoo is a global in foo.cpp. I'm just wondering if there is an g++ option that will retain the value of a global variable cfoo.

And also please take note that the application will work if I change the cfoo to hold the reference, like this,

```
//foo.cpp
CCustomClass& cfoo = CStaticClass::cfoo_static; // cfoo_static is a static member object

```

But I want to use the first example.

Please advice. Thanks.

Answer: 
I suspect you might be experiencing the [static initialization order fiasco](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14). You can solve it with the [construct on first use idiom](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15). Wrap `cfoo` in a function that initializes the value the first time you call the function. That way, it's guaranteed to be initialized when you need it. Right now, you're getting the value of `cfoo_static` before it has been initialized.
you may want to use a singleton desing patten and do something like this:

```
CcustomClass* getFoo(void) {
    static CcustomClass* cfoo = null;
    if (cfoo == null) {
        cfoo = new CcustomClass();
    }
    return cfoo;
}

```

and then call getFoo every time you want to use it.