C++單例類(lèi)模板詳解
單例類(lèi)
描述
指在整個(gè)系統(tǒng)生命期中,一個(gè)類(lèi)最多只能有一個(gè)實(shí)例(instance)存在,使得該實(shí)例的唯一性(實(shí)例是指一個(gè)對(duì)象指針) , 比如:統(tǒng)計(jì)在線(xiàn)人數(shù)
在單例類(lèi)里,又分為了懶漢式和餓漢式,它們的區(qū)別在于創(chuàng)建實(shí)例的時(shí)間不同:
- 懶漢式 : 指代碼運(yùn)行后,實(shí)例并不存在,只有當(dāng)需要時(shí),才去創(chuàng)建實(shí)例(適用于單線(xiàn)程)
- 餓漢式 : 指代碼一運(yùn)行,實(shí)例已經(jīng)存在,當(dāng)時(shí)需要時(shí),直接去調(diào)用即可(適用于多線(xiàn)程)
用法
- 將構(gòu)造函數(shù)的訪問(wèn)屬性設(shè)置為private,
- 提供一個(gè)GetInstance()靜態(tài)成員函數(shù),只能供用戶(hù)訪問(wèn)唯一一個(gè)實(shí)例.
- 定義一個(gè)靜態(tài)成員指針,用來(lái)供用戶(hù)獲取
- 重載 (=)賦值操作符以及拷貝構(gòu)造函數(shù),并設(shè)為private, 避免對(duì)象間拷貝,復(fù)制.
初探單例類(lèi)-懶漢式:
#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è)類(lèi)最多只能有一個(gè)實(shí)例(instance)存在.
注意:由于實(shí)例(instance),在系統(tǒng)生命期中,都是存在的,所以只要系統(tǒng)還在運(yùn)行,就不需要delete
上面的懶漢式如果在多線(xiàn)程情況下 ,多個(gè)Csingleton指針對(duì)象同時(shí)調(diào)用getInstance()成員函數(shù)時(shí),由于m_pInstance = NULL,就會(huì)創(chuàng)建多個(gè)實(shí)例出來(lái).
所以,在多線(xiàn)程情況下,需要使用餓漢實(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;
單例類(lèi)模板
我們現(xiàn)在講解的僅僅是個(gè)框架,里面什么都沒(méi)有,不能滿(mǎn)足需求啊,所以還要寫(xiě)為單例類(lèi)模板頭文件,當(dāng)需要單例類(lèi)時(shí),直接聲明單例類(lèi)模板頭文件即可
寫(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è)單例類(lèi)模板時(shí),只需要在自己類(lèi)里通過(guò)friend添加為友元即可,
接下來(lái)試驗(yàn)單例類(lèi)模板
寫(xiě)main.cpp
#include <iostream>
#include <string>
#include "CSingleton.h"
using namespace std;
class Test
{
friend class CSingleton<Test> ; //聲明Test的友元為單例類(lèi)模板
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++單例類(lèi)模板詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C++中運(yùn)算符重載的規(guī)則語(yǔ)法實(shí)例
今天小編就為大家分享一篇關(guān)于C++中運(yùn)算符重載的規(guī)則語(yǔ)法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 棧的基礎(chǔ)操作
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 棧的基礎(chǔ)操作的相關(guān)資料,需要的朋友可以參考下2017-05-05
C語(yǔ)言深入探究動(dòng)態(tài)規(guī)劃之線(xiàn)性DP
線(xiàn)性動(dòng)態(tài)規(guī)劃,是較常見(jiàn)的一類(lèi)動(dòng)態(tài)規(guī)劃問(wèn)題,其是在線(xiàn)性結(jié)構(gòu)上進(jìn)行狀態(tài)轉(zhuǎn)移,這類(lèi)問(wèn)題不像背包問(wèn)題、區(qū)間DP等有固定的模板,線(xiàn)性動(dòng)態(tài)規(guī)劃的目標(biāo)函數(shù)為特定變量的線(xiàn)性函數(shù),約束是這些變量的線(xiàn)性不等式或等式,目的是求目標(biāo)函數(shù)的最大值或最小值2022-04-04

