C++核心准则NL.16:使用常规的类成员声明顺序
发布日期:2021-07-01 05:30:51 浏览次数:2 分类:技术文章

本文共 2343 字,大约阅读时间需要 7 分钟。

NL.16: Use a conventional class member declaration order

NL.16:使用常规的类成员声明顺序

 

Reason(原因)

A conventional order of members improves readability.

常规的成员顺序可以提高可读性。

When declaring a class use the following order

当声明一个类时,使用以下顺序

  • types: classes, enums, and aliases (using)

    类型:类,枚举和别名(using)

  • constructors, assignments, destructor

    构造函数,赋值,析构函数

  • functions

    成员函数

  • data

    数据成员

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.

当您没有其他约束或更好的主意时,考虑这个建议。此规则可以作为许多准则之外的要求。

 

Example(示例)

class X {public:    // interfaceprotected:    // unchecked function for use by derived class implementationsprivate:    // 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.

有时,成员的默认顺序与将公共接口与实现细节分开的期望相冲突。在这种情况下,可以将私有类型和函数与私有数据一起放置。

class X {public:    // interfaceprotected:    // unchecked function for use by derived class implementationsprivate:    // 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).

避免将一个访问权限(例如公共)的多个声明块分散在具有不同访问权限(例如私有)的声明块之间。

class X {   // badpublic:    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.

使用宏声明成员组通常会导致违反所有排序规则。但是,宏仍然掩盖了正在表达的内容。

 

Enforcement(实施建议)

Flag departures from the suggested order. There will be a lot of old code that doesn't follow this rule.

标记偏离建议的顺序。会有很多旧代码没有遵循此规则。

 

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl16-use-a-conventional-class-member-declaration-order

 

新书介绍

是作者最近出版的新书,拜托多多关注!

图片

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

 


 

觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

 

图片

 

 

转载地址:https://oothinking.blog.csdn.net/article/details/115030161 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++核心准则NL.15:谨慎使用空格
下一篇:C++核心准则NL.11:使字面值可读

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月27日 03时32分11秒