实例构造函数单例模式
本篇文章朋友在北京游玩的时候突然想到的...这段时间就有想写几篇关于实例构造函数的笔记,所以回家到之后就奋笔疾书的写出来发表了
class CSingleton { public: static CSingleton * GetInstance() { if (m_pInstance == NULL) { m_pInstance = new CSingleton; } return m_pInstance; } private: CSingleton(){}; /* 默认的构造函数设置为私有 */ static CSingleton *m_pInstance; };
下面我们对该类进行测试:
#include <iostream> using namespace std; class CSingleton { public: static CSingleton * GetInstance(); int GetVar(); void SetVar(int other); private: CSingleton(); /* 默认的构造函数设置为私有 */ ~CSingleton(); static CSingleton *m_pInstance; int m_var; }; CSingleton::CSingleton() : m_var(20) { cout << "Singleton Constructor!" << endl; } CSingleton::~CSingleton() { cout << "Singleton Destructor!" << endl; } CSingleton* CSingleton::m_pInstance = NULL; /* 初始化为NULL */ CSingleton * CSingleton::GetInstance() { if (m_pInstance == NULL) { m_pInstance = new CSingleton; } return m_pInstance; } int CSingleton::GetVar() { return m_var; } void CSingleton::SetVar(int other) { m_var = other; } int main() { CSingleton *ton1 = CSingleton::GetInstance(); CSingleton *ton2 = CSingleton::GetInstance(); cout << "ton1 var = " << ton1->GetVar() << endl; cout << "ton2 var = " << ton1->GetVar() << endl; ton1->SetVar(150); cout << "ton1 var = " << ton1->GetVar() << endl; cout << "ton2 var = " << ton2->GetVar() << endl; return 0; }
执行结果为:
我们知道,程序在结束的时候,系统会主动析构全部的全局变量。事实上,系统也会析构全部的类的静态成员变量,就像这些静态成员也是全局变量一样。利用这个特征,我们可以在C++单例模式类中定义一个这样的静态成员变量,而它的独一任务就是在析构函数中删除单例类的实例。如下面的代码中的CGarbo类(Garbo意为垃圾工人):
#include <iostream> usng namespace std; class CSingleton { public: static CSingleton * GetInstance() { if (m_pInstance == NULL) { m_pInstance = new CSingleton(); } return m_pInstance; } private: CSingleton(){}; /* 默认的构造函数设置为私有 */ static CSingleton *m_pInstance; class CGarbo { public: CGarbo() { cout << "CGarbo Constructor!" << endl; } ~CGarbo() { if (CSingleton::m_pInstance != NULL) { delete m_pInstance; m_pInstance = NULL; cout << "CGarbo Destructor!" << endl; } } }; static CGarbo garbo; };
#include <iostream> using namespace std; class CSingleton { public: static CSingleton * GetInstance(); int GetVar(); void SetVar(int other); private: CSingleton(); /* 默认的构造函数设置为私有 */ ~CSingleton(); static CSingleton *m_pInstance; int m_var; class CGarbo { public: CGarbo() { cout << "CGarbo Constructor!" << endl; } ~CGarbo() { if (CSingleton::m_pInstance != NULL) { delete m_pInstance; m_pInstance = NULL; cout << "CGarbo Destructor!" << endl; } } }; static CGarbo garbo; }; CSingleton::CGarbo CSingleton::garbo; CSingleton::CSingleton() : m_var(20) { cout << "Singleton Constructor!" << endl; } CSingleton::~CSingleton() { cout << "Singleton Destructor!" << endl; } CSingleton* CSingleton::m_pInstance = NULL; CSingleton * CSingleton::GetInstance() { if (m_pInstance == NULL) { m_pInstance = new CSingleton; } return m_pInstance; } int CSingleton::GetVar() { return m_var; } void CSingleton::SetVar(int other) { m_var = other; } int main() { CSingleton *ton1 = CSingleton::GetInstance(); CSingleton *ton2 = CSingleton::GetInstance(); cout << "ton1 var = " << ton1->GetVar() << endl; cout << "ton2 var = " << ton1->GetVar() << endl; ton1->SetVar(150); cout << "ton1 var = " << ton1->GetVar() << endl; cout << "ton2 var = " << ton2->GetVar() << endl; return 0; }
执行结果为:
文章结束给大家分享下程序员的一些笑话语录:
Bphone之你们聊,我先走了!移动说:我在phone前加o,我叫o缝;苹果说:我在phone前i,我是i缝;微软说:我在phone前加w,我叫w缝;三星说:你们聊,我先走了!
将来王建宙写回忆录的时候,一定要有一句“常小兵为中国移动的发展做出了不可磨灭的贡献”。
--------------------------------- 原创文章 By 实例和构造函数 ---------------------------------