I already have some singleton classes which uses pointers:
class Logger
{
public:
static Logger* Instance()
{
if (!m_pInstance) m_pInstance = new Logger;
return m_pInstance;
}
private:
Logger();
Logger(Logger const&);
Logger& operator=(Logger const&);
static Logger* m_pInstance;
};
But, there is a simpler way, using references:
class Logger
{
public:
static Logger& Instance()
{
static Logger theLogger;
return theLogger;
}
private:
Logger();
Logger(Logger const&);
Logger& operator=(Logger const&);
~Logger();
};
Reading an article C++ Singleton design pattern, it warns about second way:
[Potential Pitfall]: This form of the singleton can present a problem because of the life expectancy of the object. If one singleton is instantiated within another, one must be keenly aware of the destructor call sequence.
But I can not understand it, Can someone show me a bad usage of it that I should avoid it?