I assume that the static object has the scope of the file (it is outside the function / class definition). You can move it to an access function and access it only through this function, for example:
class Object;
Object& getObject()
{
static Object object;
return object;
}
A static instance of the object will be initialized the first time the getObject () method is called. If the constructor of an object throws, you can easily catch an exception. You just need to remember that every call to getObject () is included in the try / catch block (or suffers from an exception that creates a stack chain); this may be a little inconvenient, but on the other hand, you can only solve the “first” call logically if you know which one is in the program logic stream.
source
share