C++單例類模板詳解
單例類
描述
指在整個(gè)系統(tǒng)生命期中,一個(gè)類最多只能有一個(gè)實(shí)例(instance)存在,使得該實(shí)例的唯一性(實(shí)例是指一個(gè)對(duì)象指針) , 比如:統(tǒng)計(jì)在線人數(shù)
在單例類里,又分為了懶漢式和餓漢式,它們的區(qū)別在于創(chuàng)建實(shí)例的時(shí)間不同:
- 懶漢式 : 指代碼運(yùn)行后,實(shí)例并不存在,只有當(dāng)需要時(shí),才去創(chuàng)建實(shí)例(適用于單線程)
- 餓漢式 : 指代碼一運(yùn)行,實(shí)例已經(jīng)存在,當(dāng)時(shí)需要時(shí),直接去調(diào)用即可(適用于多線程)
用法
- 將構(gòu)造函數(shù)的訪問(wèn)屬性設(shè)置為private,
- 提供一個(gè)GetInstance()靜態(tài)成員函數(shù),只能供用戶訪問(wèn)唯一一個(gè)實(shí)例.
- 定義一個(gè)靜態(tài)成員指針,用來(lái)供用戶獲取
- 重載 (=)賦值操作符以及拷貝構(gòu)造函數(shù),并設(shè)為private, 避免對(duì)象間拷貝,復(fù)制.
初探單例類-懶漢式:
#include <iostream> using namespace std; class CSingleton { private: static CSingleton* m_pInstance; CSingleton() //構(gòu)造函數(shù)為private { } CSingleton& operator = (const CSingleton& t); CSingleton(const CSingleton &); public: static CSingleton* getInstance() { if(m_pInstance==NULL) m_pInstance= new CSingleton(); return m_pInstance; } void print() { cout<<this<<endl; } }; CSingleton* CSingleton::m_pInstance = NULL; int main() { CSingleton *p1=CSingleton::getInstance(); CSingleton *p2=CSingleton::getInstance(); CSingleton *p3=CSingleton::getInstance(); p1->print(); p2->print(); p3->print(); return 0; }
運(yùn)行打印:
0x6e2d18
0x6e2d18
0x6e2d18
從打印結(jié)果可以看出,該指針對(duì)象指向的都是同一個(gè)地址,實(shí)現(xiàn)了一個(gè)類最多只能有一個(gè)實(shí)例(instance)存在.
注意:由于實(shí)例(instance),在系統(tǒng)生命期中,都是存在的,所以只要系統(tǒng)還在運(yùn)行,就不需要delete
上面的懶漢式如果在多線程情況下 ,多個(gè)Csingleton指針對(duì)象同時(shí)調(diào)用getInstance()成員函數(shù)時(shí),由于m_pInstance = NULL,就會(huì)創(chuàng)建多個(gè)實(shí)例出來(lái).
所以,在多線程情況下,需要使用餓漢實(shí)現(xiàn)
代碼如下:
class CSingleton { private: static CSingleton* m_pInstance; CSingleton() //構(gòu)造函數(shù)為private { } CSingleton& operator = (const CSingleton& t); CSingleton(const CSingleton &); public: static CSingleton* getInstance() { return m_pInstance; } }; CSingleton* CSingleton::m_pInstance = new CSingleton;
單例類模板
我們現(xiàn)在講解的僅僅是個(gè)框架,里面什么都沒(méi)有,不能滿足需求啊,所以還要寫(xiě)為單例類模板頭文件,當(dāng)需要單例類時(shí),直接聲明單例類模板頭文件即可
寫(xiě)CSingleton.h
#ifndef _SINGLETON_H_ #define _SINGLETON_H_ template <typename T> class CSingleton { private: static T* m_pInstance; CSingleton() //構(gòu)造函數(shù)為private { } public: static T* getInstance() { return m_pInstance; } }; template <typename T> T* CSingleton<T> :: m_pInstance = new T; #endif
當(dāng)我們需要這個(gè)單例類模板時(shí),只需要在自己類里通過(guò)friend添加為友元即可,
接下來(lái)試驗(yàn)單例類模板
寫(xiě)main.cpp
#include <iostream> #include <string> #include "CSingleton.h" using namespace std; class Test { friend class CSingleton<Test> ; //聲明Test的友元為單例類模板 private: string mstr; Test(): mstr("abc") { } Test& operator = (const Test& t); Test(const Test&); public: void Setmstr(string t) { mstr=t; } void print() { cout<<"mstr = "<<mstr<<endl; cout<<"this = "<<this<<endl; } }; int main() { Test *pt1 = CSingleton<Test>::getInstance(); Test *pt2 = CSingleton<Test>::getInstance(); pt1->print(); pt2->print(); pt1->Setmstr("ABCDEFG"); pt2->print(); return 0; }
mstr = abc
this = 0x2d2e30mstr = abc
this = 0x2d2e30mstr = ABCDEFG
this = 0x2d2e30
以上所述是小編給大家介紹的C++單例類模板詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C++異常處理方式實(shí)例詳解(超級(jí)詳細(xì)!)
程序有時(shí)會(huì)遇到運(yùn)行階段錯(cuò)誤,導(dǎo)致程序無(wú)法正常執(zhí)行下去,c++異常為處理這種情況提供了一種功能強(qiáng)大的而靈活的工具,下面這篇文章主要給大家介紹了關(guān)于C++異常處理方式的相關(guān)資料,需要的朋友可以參考下2023-04-04C語(yǔ)言怎么連接兩個(gè)數(shù)組的內(nèi)容你知道嗎
這篇文章主要為大家介紹了C語(yǔ)言怎么連接兩個(gè)數(shù)組的內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01

Qt+OpenCV利用幀差法實(shí)現(xiàn)車輛識(shí)別

詳解C語(yǔ)言內(nèi)核中的自旋鎖結(jié)構(gòu)