Where to use it:
You can use it whenever you need only one instance of a class.
But like all things too much isn't good. It can be alot of work to change your code when you decide to have more then one instance of your class in a later stage of development.
So be sure you'll only need one instance.
Make your constructor private.
You create a static method (preferably called: instance) and this method will call your private constructor.
this instance will keep a static member of your class and returns this member whenever it is called.
static mySingleton* instance(void);
void data(int data);}
Now you'll be able to use the getters and setters of data_ in every class/function and data() will always return the same value.
mySingleton::mySingleton() : data_(0)
static mySingleton inst;
void mySingleton::data(int data)
This is the code for a simple logging mechanism :
using namespace std;
static log* instance(void);
void initialize(string filename);
Now I only have to initialize the class once and I'll be able to write to the log file whenever I want without haveing a global variable or opening the logfile in every function/class.
log::log(void) : filename_("")
static log instance;
void log::initialize(string filename)