In my application, I have a Singleton that represents a "system." This object is created first and destroyed last, and all other objects must be managed by it, which means that each object will be created directly or indirectly by this system object (in its method run), and all objects will be (I hope) before the system is deleted.
The problem is that additional access to the system object (via a pointer) introduces a lot of non-null verification code. I know that dereferencing a null pointer results in undefined behavior. Does good old C use the assertbest I can do?
#include "MySys.h"
class MySysImplementation: public MySys
{
public:
MySysImplementation();
void run();
};
namespace {
MySys* g_sys = NULL;
}
MySys& MySys::Instance()
{
assert(g_sys);
return *g_sys;
}
int main()
{
{
MySysImplementation sys;
g_sys = &sys;
sys.run();
}
g_sys = NULL;
}