C++核心準(zhǔn)則SF.2:.h文件一定不能包含對象定義或者非inline函數(shù)定義

SF.2: A?.h?file must not contain object definitions or non-inline function definitions
SF.2:.h文件一定不能包含對象定義或者非inline函數(shù)定義
Reason(原因)
Including entities subject to the one-definition rule leads to linkage errors.
對于包含實物對象違反一次定義規(guī)則,會導(dǎo)致鏈接錯誤。
Example(示例)
// file.h:
namespace Foo {
int x = 7;
int xx() { return x+x; }
}
// file1.cpp:
#include
// ... more ...
// file2.cpp:
#include
// ... more ...
Linking?file1.cpp?and?file2.cpp?will give two linker errors.
鏈接file1.cpp和file2.cpp時會發(fā)生兩個鏈接錯誤。
Alternative formulation: A?.h?file must contain only:
另外一種表達(dá)方式:頭文件只應(yīng)該包含:
#includes of other?.h?files (possibly with include guards)
#include其它.h文件(可能包含include監(jiān)視)
templates
模板
class definitions
類定義
function declarations
函數(shù)聲明
extern?declarations
extern聲明
inline?function definitions
inline函數(shù)定義
constexpr?definitions
常量表達(dá)式定義
const?definitions
常量表達(dá)式
using?alias definitions
using別名定義
???
Enforcement(實施建議)
Check the positive list above.
檢查上面的列表。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#sf2-a-h-file-must-not-contain-object-definitions-or-non-inline-function-definitions
新書介紹
《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

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