C++核心準則?NL.16:使用常規(guī)的類成員聲明順序

NL.16: Use a conventional class member declaration order
NL.16:使用常規(guī)的類成員聲明順序
Reason(原因)
A conventional order of members improves readability.
常規(guī)的成員順序可以提高可讀性。
When declaring a class use the following order
當聲明一個類時,使用以下順序
types: classes, enums, and aliases (using)
類型:類,枚舉和別名(using)
constructors, assignments, destructor
構(gòu)造函數(shù),賦值,析構(gòu)函數(shù)
functions
成員函數(shù)
data
數(shù)據(jù)成員
Use the?public?before?protected?before?private?order.
使用公有成員處于保護成員之前,保護成員處于私有成員之前的順序。
This is a recommendation for?when you have no constraints or better ideas. This rule was added after many requests for guidance.
當您沒有其他約束或更好的主意時,考慮這個建議。此規(guī)則可以作為許多準則之外的要求。
Example(示例)
class X {
public:
// interface
protected:
// unchecked function for use by derived class implementations
private:
// implementation details
};
Example(示例)
Sometimes, the default order of members conflicts with a desire to separate the public interface from implementation details. In such cases, private types and functions can be placed with private data.
有時,成員的默認順序與將公共接口與實現(xiàn)細節(jié)分開的期望相沖突。在這種情況下,可以將私有類型和函數(shù)與私有數(shù)據(jù)一起放置。
class X {
public:
// interface
protected:
// unchecked function for use by derived class implementations
private:
// implementation details (types, functions, and data)
};Example, bad(反面示例)
Avoid multiple blocks of declarations of one access (e.g.,?public) dispersed among blocks of declarations with different access (e.g.?private).
避免將一個訪問權(quán)限(例如公共)的多個聲明塊分散在具有不同訪問權(quán)限(例如私有)的聲明塊之間。
class X { // bad
public:
void f();
public:
int g();
// ...
};
The use of macros to declare groups of members often leads to violation of any ordering rules. However, macros obscures what is being expressed anyway.
使用宏聲明成員組通常會導(dǎo)致違反所有排序規(guī)則。但是,宏仍然掩蓋了正在表達的內(nèi)容。
Enforcement(實施建議)
Flag departures from the suggested order. There will be a lot of old code that doesn't follow this rule.
標記偏離建議的順序。會有很多舊代碼沒有遵循此規(guī)則。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl16-use-a-conventional-class-member-declaration-order
新書介紹
《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

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