The main points of the singleton pattern are three; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own. From the specific implementation point of view, is the following three points: first, the Singleton mode class only provides a private constructor, the second is that the class definition contains a static private object of the class, and third, the class provides a static public function to create or get its own static private objects. #include <iostream> #include <stdlib.h>using namespace Std;class Csingleton{private:csingleton ()// Constructor privatization {cout<< "Csingleton ()" <<ENDL;} Public:static csingleton* getinstance () {static csingleton* m_pinstance;//determines if the first call to if (m_pinstance==null) {m_ Pinstance= new Csingleton ();} return m_pinstance;}; void Test () {csingleton* c=csingleton::getinstance (); csingleton* s=csingleton::getinstance ();} int main () {test (); System ("pause"); return 0;} Result: [Object Object]
This article from "Liveyoung" blog, reproduced please contact the author!
To design a class, we can only generate one instance of the class.