I have usually implemented Singletons as described in the Design Patterns book. Recently someone suggested another implementation of Singleton using creating a static instance of a class and returning that instead of using new and returning a pointer to the new singleton. I would like to get your input. Is there any advantages of using one or the other? Disadvantages? Disadvantages for the static singleton implementation?
Let me illustrate:
Traditional Singleton Implementation
Static ImplementationCode:Car* Car::getInstance() { if(!InstanceFlag) { Instance = new Car(); InstanceFlag = true; return Instance; } else { return mp_Instance; } }
Advantages & Disadvantages? Any documents or articles I can refer to for the second implementation ? Thanks for your help!Code:Car& Car::getInstance() { static Car theCar; return theCar; }




Reply With Quote
