// singleton #include class singleton { public: static singleton* getInstance() { ++refCount; std::cout << "\n refCount = " << refCount; if(pInstance == 0) return (pInstance = new singleton); else return pInstance; } void release() { if(refCount == 0) return; if(--refCount == 0) { std::cout << "\n deleting instance"; delete this; } std::cout << "\n refCount = " << refCount; } void say() { std::cout << "singleton reference #" << refCount; } private: singleton() {} ~singleton() { pInstance = 0; } static singleton *pInstance; static int refCount; }; singleton* singleton::pInstance = 0; int singleton::refCount = 0; int main() { singleton *pI1 = singleton::getInstance(); singleton *pI2 = singleton::getInstance(); std::cout << "\n address of 1st reference = " << pI1; std::cout << "\n address of 2nd reference = " << pI2; pI1->release(); pI2->release(); singleton* pI3 = singleton::getInstance(); std::cout << "\n address of 3rd reference = " << pI1; pI3->release(); std::cout << "\n\n"; return 0; }