国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C++中的Singleton類的實現(xiàn)(1)

2019-11-17 05:12:56
字體:
供稿:網(wǎng)友
《設計模式》中把 Singleton 寫成返回指針:




class Singleton{

public:

static Singleton* Instance();

PRotected:

Singleton();

private:

static Singleton* _instance;

};相應的實現(xiàn) cpp 文件是:

Singleton* Singleton::_instance;

Singleton* Singleton::Instance(){

if( _instance == 0){

_instance = new Singleton;

};

return _instance;

} 將構造函數(shù)設計成 protected 的目的是防止在 class 外面 new ,有人可能會設計成 private ,假如考慮到有可能會繼續(xù)這個類的話,還是將構造函數(shù)設計成 protected 比較好,還需要加一個 virtual 析構函數(shù)。為了防止別人復制 Singleton 對象:

Singleton* pSingleton = Singleton::Instance();

Singleton s1 = *pSingleton;

Singleton s2 = *pSingleton;

需要將拷貝構造(copy constrUCtor)函數(shù)變成 private。

但是這里存在的問題是,什么時候刪除 Singleton 對象?按照 C++ 的一個基本原則,對象在哪里創(chuàng)建就在哪里銷毀,這里還應該放一個 destroy 方法來刪除 Singleton 對象。假如忘了刪除就比較麻煩。Instance 函數(shù)還存在多線程同時訪問的加鎖問題。假如把 Instance 函數(shù)開始和結尾放上加鎖和解鎖,整個函數(shù)性能會下降很多。這不是一個好的設計。

有一個小小的改動,可以避免忘了刪除 Singleton 對象帶來內(nèi)存泄露的問題。那就是用 std:auto_ptr 來包含 Singleton 對象,定義一個class static member auto_ptr 對象,在析構的靜態(tài) auto_ptr 變量的時候時候自動刪除 Singleton 對象。為了不讓用戶 delete Singleton 對象,需要將析構函數(shù)由 public 變成 protected。以下是頭文件 SingletonAutoPtr.h :

#include <memory>

using namespace std;

class CSingletonAutoPtr

{

private:

static auto_ptr<CSingletonAutoPtr> m_auto_ptr;

static CSingletonAutoPtr* m_instance;

protected:

CSingletonAutoPtr();

CSingletonAutoPtr(const CSingletonAutoPtr&);

virtual ~CSingletonAutoPtr();

//allow auto_ptr to delete, using protected ~CSingletonAutoPtr()

friend class auto_ptr<CSingletonAutoPtr>;

public:

static CSingletonAutoPtr* GetInstance();

void Test();

}; 《設計模式》中把 Singleton 寫成返回指針:




class Singleton{

public:

static Singleton* Instance();

protected:

Singleton();

private:

static Singleton* _instance;

};相應的實現(xiàn) cpp 文件是:

Singleton* Singleton::_instance;

Singleton* Singleton::Instance(){

if( _instance == 0){

_instance = new Singleton;

};

return _instance;

} 將構造函數(shù)設計成 protected 的目的是防止在 class 外面 new ,有人可能會設計成 private ,假如考慮到有可能會繼續(xù)這個類的話,還是將構造函數(shù)設計成 protected 比較好,還需要加一個 virtual 析構函數(shù)。為了防止別人復制 Singleton 對象:

Singleton* pSingleton = Singleton::Instance();

Singleton s1 = *pSingleton;

Singleton s2 = *pSingleton;

需要將拷貝構造(copy constructor)函數(shù)變成 private。


但是這里存在的問題是,什么時候刪除 Singleton 對象?按照 C++ 的一個基本原則,對象在哪里創(chuàng)建就在哪里銷毀,這里還應該放一個 destroy 方法來刪除 Singleton 對象。假如忘了刪除就比較麻煩。Instance 函數(shù)還存在多線程同時訪問的加鎖問題。假如把 Instance 函數(shù)開始和結尾放上加鎖和解鎖,整個函數(shù)性能會下降很多。這不是一個好的設計。

有一個小小的改動,可以避免忘了刪除 Singleton 對象帶來內(nèi)存泄露的問題。那就是用 std:auto_ptr 來包含 Singleton 對象,定義一個class static member auto_ptr 對象,在析構的靜態(tài) auto_ptr 變量的時候時候自動刪除 Singleton 對象。為了不讓用戶 delete Singleton 對象,需要將析構函數(shù)由 public 變成 protected。以下是頭文件 SingletonAutoPtr.h :

#include <memory>

using namespace std;

class CSingletonAutoPtr

{

private:

static auto_ptr<CSingletonAutoPtr> m_auto_ptr;

static CSingletonAutoPtr* m_instance;

protected:

CSingletonAutoPtr();

CSingletonAutoPtr(const CSingletonAutoPtr&);

virtual ~CSingletonAutoPtr();

//allow auto_ptr to delete, using protected ~CSingletonAutoPtr()

friend class auto_ptr<CSingletonAutoPtr>;

public:

static CSingletonAutoPtr* GetInstance();

void Test();

}; QQread.com 推出游戲功略 http://www.qqread.com/netgame/game/index.Html 魔獸世界 跑跑卡丁車 街頭籃球 水滸Q傳 龍與地下城OL 征服  軒轅劍5 FIFA07 熱血江湖 大唐風云 夢幻西游 武林外傳 《設計模式》中把 Singleton 寫成返回指針:




class Singleton{

public:

static Singleton* Instance();

protected:

Singleton();

private:

static Singleton* _instance;

};相應的實現(xiàn) cpp 文件是:

Singleton* Singleton::_instance;

Singleton* Singleton::Instance(){

if( _instance == 0){

_instance = new Singleton;

};

return _instance;

}
將構造函數(shù)設計成 protected 的目的是防止在 class 外面 new ,有人可能會設計成 private ,假如考慮到有可能會繼續(xù)這個類的話,還是將構造函數(shù)設計成 protected 比較好,還需要加一個 virtual 析構函數(shù)。為了防止別人復制 Singleton 對象:

Singleton* pSingleton = Singleton::Instance();

Singleton s1 = *pSingleton;

Singleton s2 = *pSingleton;

需要將拷貝構造(copy constructor)函數(shù)變成 private。

但是這里存在的問題是,什么時候刪除 Singleton 對象?按照 C++ 的一個基本原則,對象在哪里創(chuàng)建就在哪里銷毀,這里還應該放一個 destroy 方法來刪除 Singleton 對象。假如忘了刪除就比較麻煩。Instance 函數(shù)還存在多線程同時訪問的加鎖問題。假如把 Instance 函數(shù)開始和結尾放上加鎖和解鎖,整個函數(shù)性能會下降很多。這不是一個好的設計。

有一個小小的改動,可以避免忘了刪除 Singleton 對象帶來內(nèi)存泄露的問題。那就是用 std:auto_ptr 來包含 Singleton 對象,定義一個class static member auto_ptr 對象,在析構的靜態(tài) auto_ptr 變量的時候時候自動刪除 Singleton 對象。為了不讓用戶 delete Singleton 對象,需要將析構函數(shù)由 public 變成 protected。以下是頭文件 SingletonAutoPtr.h :

#include <memory>

using namespace std;

class CSingletonAutoPtr

{

private:

static auto_ptr<CSingletonAutoPtr> m_auto_ptr;

static CSingletonAutoPtr* m_instance;

protected:

CSingletonAutoPtr();

CSingletonAutoPtr(const CSingletonAutoPtr&);

virtual ~CSingletonAutoPtr();

//allow auto_ptr to delete, using protected ~CSingletonAutoPtr()

friend class auto_ptr<CSingletonAutoPtr>;

public:

static CSingletonAutoPtr* GetInstance();

void Test();

}; QQread.com 推出游戲功略 http://www.qqread.com/netgame/game/index.html 魔獸世界 跑跑卡丁車 街頭籃球 水滸Q傳 龍與地下城OL 征服  軒轅劍5 FIFA07 熱血江湖 大唐風云 夢幻西游 武林外傳

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 赤城县| 安岳县| 鄂州市| 安多县| 隆德县| 利川市| 宜昌市| 徐州市| 遵化市| 安远县| 荆州市| 曲松县| 治县。| 始兴县| 筠连县| 商丘市| 南通市| 新竹县| 盐亭县| 南澳县| 额尔古纳市| 寻乌县| 灵川县| 东光县| 崇义县| 利津县| 南川市| 汉阴县| 灵石县| 上杭县| 海阳市| 蚌埠市| 池州市| 滦南县| 永福县| 略阳县| 葫芦岛市| 河源市| 阳朔县| 八宿县| 贺州市|