SF.2: A .h file must not contain object definitions or non-inline function definitions
SF.2:.h文件一定不能包含对象定义或者非inline函数定义
Reason(原因)
Including entities subject to the one-definition rule leads to linkage errors.
对于包含实物对象违反一次定义规则,会导致链接错误。
Example(示例)
// file.h:
namespace Foo {
int x = 7;
int xx() { return x+x; }
}
// file1.cpp:
#include <file.h>
// ... more ...
// file2.cpp:
#include <file.h>
// ... more ...
Linking file1.cpp and file2.cpp will give two linker errors.
链接file1.cpp和file2.cpp时会发生两个链接错误。
Alternative formulation: A .h file must contain only:
另外一种表达方式:头文件只应该包含:
- #includes of other .h files (possibly with include guards)
#include其它.h文件(可能包含include监视) - templates
模板 - class definitions
类定义 - function declarations
函数声明 - extern declarations
extern声明 - inline function definitions
inline函数定义 - constexpr definitions
常量表达式定义 - const definitions
常量表达式 - 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
新书介绍
《实战Python设计模式》是作者最近出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!