java 成员变量声明顺序_C++核心准则讨论:按照成员声明的顺序定义和初始化成员变量...
Discussion: Define and initialize member variables in the order of member declaration
討論:按照成員聲明的順序定義和初始化成員變量
Member variables are always initialized in the order they are declared in the class definition, so write them in that order in the constructor initialization list. Writing them in a different order just makes the code confusing because it won't run in the order you see, and that can make it hard to see order-dependent bugs.
成員變量總是按照它們在類定義中聲明的順序進(jìn)行初始化,因此請按該順序?qū)⑵鋵懭霕?gòu)造函數(shù)初始化列表中。以不同的順序編寫它們只會使代碼令人困惑,因?yàn)樗粫凑漳吹降捻樞蜻\(yùn)行,并且這使得很難看到與順序相關(guān)的錯誤。
class Employee {string email, first, last;
public:
Employee(const char* firstName, const char* lastName);
// ...
};
Employee::Employee(const char* firstName, const char* lastName)
: first(firstName),
last(lastName),
// BAD: first and last not yet constructed
email(first + "." + last + "@acme.com")
{}
In this example,?email?will be constructed before?first?and?last?because it is declared first. That means its constructor will attempt to use?first?and?last?too soon -- not just before they are set to the desired values, but before they are constructed at all.
在此示例中,由于email對象首先被聲明,所以將在first和last之前進(jìn)行構(gòu)造。這意味著它的構(gòu)造函數(shù)試圖過早使用first和last-不僅早于將它們設(shè)置為所需值之前,甚至?xí)庥鰧ο笸耆珮?gòu)造之前。
If the class definition and the constructor body are in separate files, the long-distance influence that the order of member variable declarations has over the constructor's correctness will be even harder to spot.
如果類定義和構(gòu)造函數(shù)體位于不同的文件中,則成員變量聲明的順序?qū)?gòu)造函數(shù)正確性的遠(yuǎn)程影響將更加難以發(fā)現(xiàn)。
References(參考):
[Cline99]?§22.03-11,?[Dewhurst03]?§52-53,?[Koenig97]?§4,?[Lakos96]?§10.3.5,?[Meyers97]?§13,?[Murray93]?§2.1.3,?[Sutter00]?§47
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-define-and-initialize-member-variables-in-the-order-of-member-declaration
新書介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書,拜托多多關(guān)注!
本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計(jì)模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實(shí)的軟件開發(fā)工作中每個設(shè)計(jì)模式的運(yùn)用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。
對設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運(yùn)用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計(jì)和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎?#xff01;
總結(jié)
以上是生活随笔為你收集整理的java 成员变量声明顺序_C++核心准则讨论:按照成员声明的顺序定义和初始化成员变量...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab的默认字体_matlab默认
- 下一篇: 后台接收数组_「面试必备」最新整理出的腾