// implementation of a singleton #include "singleton.h" #include <iostream> using namespace std; // initialize pointer Singleton* Singleton::pinstance = 0; /* // Thread safe singleton implementation // however, you must delete what you use Singleton* Singleton::Instance () { // is it the first call? if (pinstance == 0) { // create sole instance pinstance = new Singleton; } // address of sole instance return pinstance; } */ // simple non-threaded solution // this cleans up after itself Singleton* Singleton::Instance () { static Singleton inst; return &inst; } Singleton::Singleton() { //... perform necessary instance initializations cout << "initializing a singleton class" << endl; }
Hosted by www.Geocities.ws

1