C 中的 Singleton 實現

2021-09-30 02:25:38 字數 3219 閱讀 6331

c++ 中的 singleton 實現

關鍵字:ansi c++, singleton, static member, initialize, auto_ptr, std, stl , implement, 實現

ansi c++ 中的 singleton 實現說難不難,說容易也不容易,很多人寫 ansi c++ 的 singleton class 都有錯誤。這篇文章討論怎樣在 ansi c++ 中寫 singleton class, 希望對大家有幫助。

《設計模式》中把 singleton 寫成返回指標:

class singleton;

相應的實現 cpp 檔案是:

singleton* singleton::_instance;

singleton* singleton::instance();

return _instance;}

將建構函式設計成 protected 的目的是防止在 class 外面 new ,有人可能會設計成 private ,如果考慮到有可能會繼承這個類的話,還是將建構函式設計成 protected 比較好,還需要加乙個 virtual 析構函式。為了防止別人複製 singleton 物件:

singleton* psingleton = singleton::instance();

singleton s1 = *psingleton;

singleton s2 = *psingleton;

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

但是這裡存在的問題是,什麼時候刪除 singleton 物件?按照 c++ 的乙個基本原則,物件在**建立就在**銷毀,這裡還應該放乙個 destroy 方法來刪除 singleton 物件。如果忘了刪除就比較麻煩。instance 函式還存在多執行緒同時訪問的加鎖問題。如果把 instance 函式開始和結尾放上加鎖和解鎖,整個函式效能會下降很多。這不是乙個好的設計。

有乙個小小的改動,可以避免忘了刪除 singleton 物件帶來記憶體洩露的問題。那就是用 std:auto_ptr 來包含 singleton 物件,定義乙個class static member auto_ptr 物件,在析構的靜態 auto_ptr 變數的時候時候自動刪除 singleton 物件。為了不讓使用者 delete singleton 物件,需要將析構函式由 public 變成 protected。以下是標頭檔案 singletonautoptr.h :

#include

using namespace std;

class csingletonautoptr

;

對應的 singletonautoptr.cpp 如下:

#include "singletonautoptr.h"

#include

//initial static member vars here

csingletonautoptr* csingletonautoptr::m_instance = null;

auto_ptr

csingletonautoptr::m_auto_ptr;

//// construction/destruction

//csingletonautoptr::csingletonautoptr()

csingletonautoptr::~csingletonautoptr()

csingletonautoptr* csingletonautoptr::getinstance()

void csingletonautoptr::test()

呼叫方法:

csingletonautoptr* psingleton = csingletonautoptr::getinstance();

psingleton->test();

寫乙個 c++ 中的 singleton 需要這麼費勁,大大出乎我們的意料。有很多人從未用過 auto_ptr,而且 std:auto_ptr 本身就並不完美,它是基於物件所有權機制的,相比之下,apache log4cxx 中有乙個 auto_ptr, 是基於物件計數的,更為好用。只是為了用乙個好的 auto_ptr 而不得不用 log4cxx , 對於很多專案來說,也不太好。當然了,ansi c++ 的 stl 中 std:auto_ptr 對於寫上面的例子已經足夠用了。

另外乙個思路是,把 getinstance 函式設計成 static member 可能更好,因為一般來說,singleton 物件都不大,static member 雖然必須一直占用記憶體,問題不大。這裡的析構函式必須設成 public 了。以下是標頭檔案 singlestaticobj.h

class csingletonstaticobj

;對應的 singlestaticobj.cpp 檔案為:

#include "singletonstaticobj.h"

#include

#include

using namespace std;

csingletonstaticobj csingletonstaticobj::m_instance;

csingletonstaticobj::csingletonstaticobj()

csingletonstaticobj::~csingletonstaticobj()

csingletonstaticobj& csingletonstaticobj::getinstance()

void csingletonstaticobj::test()

呼叫方法:

csingletonstaticobj& singleton = csingletonautoptr::getinstance();

singleton.test();

從**量來說,似乎使用 static member ref 更為簡單。我更偏向於用這種方法。

但是,並不是所有情況下面都適合用 static member singleton。比如說,getinstance 需要動態決定返回不同的 instance 的時候,就不能用。舉例來說,filesystem::getinstance, 在 windows 下面執行可能需要返回 new winfilesystem, linux/unix 下面執行可能需要返回 new linuxfilesystem,這個時候還是需要用上面的 auto_ptr 包含 singleton 指標的方法。

怎麼寫 singleton 要看大家的專案需要。

以上**在 visual c++ 6.0 下面編譯通過。

C 中的 Singleton 實現

ansi c 中的 singleton 實現說難不難,說容易也不容易,很多人寫 ansi c 的 singleton class 都有錯誤。這篇文章討論怎樣在 ansi c 中寫 singleton class,希望對大家有幫助。設計模式 中把 singleton 寫成返回指標 class sing...

C 中的Singleton 類的實現

ansi c 中的 singleton 實現說難不難,說容易也不容易,很多人寫 ansi c 的 singleton class 都有錯誤。這篇文章討論怎樣在 ansi c 中寫 singleton class,希望對大家有幫助。設計模式 中把 singleton 寫成返回指標 class sing...

C 中實現Singleton的正確方法

如果某個類管理了系統中唯一的某種資源,那麼我們只能建立該類的乙個例項,此時用到singleton設計模式 後面為了簡化將省略 設計模式 四個字 就比較合適了。然而,如果不注意實現方法,就很有可能會讓我們碰到一些莫名其妙的錯誤。圖1是經過簡化所得到的乙個實現錯誤的例子。main.c00001 incl...