Re: Assign Reference to another Referance
I'll give you a compilable example as
soon as I get VS fixed later today.
Good! Please do so!
class Singleton
{
public:
static Singleton & Instance()
{
if( !m_instance )
{
m_instance = new Singleton();
}
return *m_instance;
}
static void DoStuff()
{
int x = 1 + 1;
}
private:
static Singleton * m_instance;
};
Singleton * Singleton::m_instance = 0;
class Foo
{
public:
Foo(){}
~Foo()
{
Singleton::Instance().DoStuff();
}
};
int main()
{
static Foo foo;
return 0; // Undefined behavior after this line, when program
cleanup occurs!
}
static de-initialization fiasco.
If you were the only person working on a project, it is avoidable as
long as you are careful. However, I've yet to land a job where I was
the one and only programmer to ever touch the code, nor do I think I'd
want to. You will undoubtedly run into someone that directly or
indirectly creates this scenario when you introduce singletons into
your project. Thus, my argument is that it is far simpler and less
error prone to simply pass a reference to the would be singleton
object around to those who need it and check the address when needed.