這篇文章主要講解了“C++怎么避免單例”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C++怎么避免單例”吧!
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、昭平網(wǎng)絡(luò)推廣、微信小程序開(kāi)發(fā)、昭平網(wǎng)絡(luò)營(yíng)銷、昭平企業(yè)策劃、昭平品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供昭平建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
Singletons are basically complicated global objects in disguise.
單例基本上就是一個(gè)復(fù)雜的全局對(duì)象的偽裝。
Example(示例)
class Singleton { // ... lots of stuff to ensure that only one Singleton object is created, // that it is initialized properly, etc.};
There are many variants of the singleton idea. That's part of the problem.
存在許多單例想法的版本。這是問(wèn)題的一個(gè)方面。
If you don't want a global object to change, declare it const
or constexpr
.
如果你不希望全局對(duì)象發(fā)生改變,將其定義為常量或常量表達(dá)式。
Exception(例外)
You can use the simplest "singleton" (so simple that it is often not considered a singleton) to get initialization on first use, if any:
你可以使用最簡(jiǎn)單的“單例”(簡(jiǎn)單到經(jīng)常不被認(rèn)為是單例)以便在第一次使用時(shí)獲取初始值。
X& myX(){ static X my_x {3}; return my_x;}
這是解決初始化順序相關(guān)問(wèn)題的最有效手段。在多線程環(huán)境中,靜態(tài)對(duì)象的的初始化不會(huì)觸發(fā)競(jìng)爭(zhēng)條件(除非你不小心在某個(gè)共享對(duì)象的構(gòu)造函數(shù)中訪問(wèn)了它)
譯者注:這也是目前單例設(shè)計(jì)模式在現(xiàn)代C++中最新實(shí)現(xiàn)。
注意局部的靜態(tài)變量不包含競(jìng)爭(zhēng)條件。然而,如果X的析構(gòu)動(dòng)作牽涉到需要被同步的操作,我們必須使用一個(gè)次簡(jiǎn)單的解決方案。例如:
X& myX(){ static auto p = new X {3}; return *p; // potential leak}
Now someone must delete
that object in some suitably thread-safe way. That's error-prone, so we don't use that technique unless
現(xiàn)在某個(gè)角色必須以一種合適的線程安全的方式刪除那個(gè)對(duì)象。由于容易引發(fā)錯(cuò)誤,因此我們不會(huì)使用這個(gè)技術(shù),除非
myX
is in multi-threaded code,
myX就在多線程代碼中
that X
object needs to be destroyed (e.g., because it releases a resource), and
X對(duì)象需要被銷毀(例如,因?yàn)樗尫刨Y源)
X
's destructor's code needs to be synchronized.
X的析構(gòu)函數(shù)的代碼需要被同步。
譯者注:函數(shù)內(nèi)部構(gòu)建的靜態(tài)對(duì)象在程序退出時(shí)會(huì)被自動(dòng)析構(gòu),如果希望自己控制析構(gòu)的時(shí)機(jī)就只能動(dòng)態(tài)構(gòu)建對(duì)象以避開(kāi)自動(dòng)析構(gòu)。
If you, as many do, define a singleton as a class for which only one object is created, functions like myX
are not singletons, and this useful technique is not an exception to the no-singleton rule.
如果你像很多人一樣為只創(chuàng)建一個(gè)的對(duì)象定義單例類,像myX那樣的函數(shù)沒(méi)有使用單例,這個(gè)有用的技術(shù)也不是禁止單例規(guī)格的例外。
譯者注:做個(gè)做法不是單例,也就不會(huì)違反禁止單例的規(guī)則。
Enforcement(實(shí)施建議)
Very hard in general.
通常會(huì)十分困難。
Look for classes with names that include singleton
.
找到名稱中包含singletong的類。
Look for classes for which only a single object is created (by counting objects or by examining constructors).
找到只創(chuàng)建一個(gè)對(duì)象的類(通過(guò)對(duì)對(duì)象計(jì)數(shù)或檢查(監(jiān)控)構(gòu)造函數(shù))
If a class X has a public static function that contains a function-local static of the class' type X and returns a pointer or reference to it, ban that.
如果類X有一個(gè)公開(kāi)的靜態(tài)函數(shù),在其內(nèi)部存在一個(gè)類型為X、作用域?yàn)樵摵瘮?shù)范圍的局部靜態(tài)變量并返回這個(gè)變量的指針或引用時(shí),禁止它。
感謝各位的閱讀,以上就是“C++怎么避免單例”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C++怎么避免單例這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!