C++核心準則討論:如果一個類是資源句柄,則它需要一個構(gòu)造函數(shù),一個析構(gòu)函數(shù)以及復(fù)制和/或移動操作

Discussion: If a class is a resource handle, it needs a constructor, a destructor, and copy and/or move operations
討論:如果一個類是資源句柄,則它需要一個構(gòu)造函數(shù),一個析構(gòu)函數(shù)以及復(fù)制和/或移動操作
Reason(原因)
To provide complete control of the lifetime of the resource. To provide a coherent set of operations on the resource.
提供對資源生命周期的完全控制。在資源上提供連貫的操作集。
Example(示例)
??? Messing with pointers
Note(注意)
If all members are resource handles, rely on the default special operations where possible.
如果所有成員都是資源句柄,請盡可能依靠默認的特殊操作。
template struct Named {
string name;
T value;
};
Now?Named?has a default constructor, a destructor, and efficient copy and move operations, provided?T?has.
現(xiàn)在,Named類具有默認的構(gòu)造函數(shù),析構(gòu)函數(shù)以及有效的復(fù)制和移動操作(如果T具有)。
Enforcement(實施建議)
In general, a tool cannot know if a class is a resource handle. However, if a class has some of?the default operations, it should have all, and if a class has a member that is a resource handle, it should be considered as resource handle.
通常,工具無法知道類是否是資源句柄。但是,如果類具有某些默認操作,則應(yīng)具有全部默認操作,并且如果類具有作為資源句柄的成員,則應(yīng)將其視為資源句柄。
原文鏈接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-if-a-class-is-a-container-give-it-an-initializer-list-constructor
新書介紹
《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。
對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
