Image

Imagesilveruby wrote in Imagecpp

singleton class question

I have to make a logger class using singleton pattern for a class and ran upon several versions. As a C++ newbie... I can't figure out which one works best.


>>> Ver. ONE

logger.h #ifndef LOGGER_H #define LOGGER_H class Logger { public: static Logger* Instance(); // other methods protected: Logger(); private: static Logger* _instance; }; #endif ------------- file.cpp Logger* Logger::_instance = 0; Logger* Logger::Instance () { if (_instance ==0) { _instance = new Logger; } return _instance; }

-------------
>>> Ver. TWO

http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-2/1/

The solution... singleton.h

-------------
>>> Ver. THREE

http://www.codeguru.com/cpp/tic/tic0287.shtml

this doesn't use a private constructor...

=======================

Each claims that the singleton pattern is very simple but they all gave different patterns O_o
Version two and three uses (&) and the first uses (*)...
Meh, I really can't figure out what's the difference between type& object and type* object. Doesn't (&) and (*) both points to a certain address? What is difference between a reference operator and a pointer?

(&) means the address of... when it's &object, I know it's address of the object, but type& object? What is the initial value of object when it's declared?

and (*) just declares a pointer pointing toward a specific type. Then when we create type* object, is the initial value NULL?

Could someone explain? Thank you!