单例:可以获得全局唯一对象
#include <iostream>
#include <stdlib.h>
using namespace std;
class Singleton{
private:
Singleton(){
m_singer = NULL;
m_count = 0;
cout << "构造函数" << endl;
}
public:
static Singleton *getInstance(){
if (m_singer == NULL){
m_singer = new Singleton;
}
return m_singer;
}
static void printT(){
cout << m_count << endl;
}
private:
static Singleton *m_singer;
static int m_count;
};
Singleton *Singleton::m_singer = 0;
int Singleton::m_count = 0;
void main(){
cout << "演示单例" << endl;
Singleton *p1 = Singleton::getInstance();
p1->printT();
system("pause");
}