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.

成员变量总是按照它们在类定义中声明的顺序进行初始化,因此请按该顺序将其写入构造函数初始化列表中。以不同的顺序编写它们只会使代码令人困惑,因为它不会按照您看到的顺序运行,并且这使得很难看到与顺序相关的错误。

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之前进行构造。这意味着它的构造函数试图过早使用first和last-不仅早于将它们设置为所需值之前,甚至会遭遇对象完全构造之前。

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.

如果类定义和构造函数体位于不同的文件中,则成员变量声明的顺序对构造函数正确性的远程影响将更加难以发现。

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

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

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

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


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

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

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

java 成员变量声明顺序_C++核心准则讨论:按照成员声明的顺序定义和初始化成员变量...相关推荐

  1. c++ file* 句柄泄漏_C++核心准则?讨论:持有没有被句柄管理的资源时切勿抛出异常...

    Discussion: Never throw while holding a resource not owned by a handle 讨论:持有没有被句柄管理的资源时切勿抛出异常 Reason ...

  2. c++ h cpp文件如何关联_C++核心准则SF.5: .cpp文件必须包含定义它接口的.h文件

    SF.5: A .cpp file must include the .h file(s) that defines its interface SF.5: .cpp文件必须包含定义它接口的.h文件 ...

  3. modbus软件开发实战指南_C++核心准则?GSL:指南支持库

    GSL: Guidelines support library GSL:指南支持库 The GSL is a small library of facilities designed to suppo ...

  4. c 函数多次声明_C++核心准则C.132:不要没有理由就将函数声明为虚函数

    岫玉 C.132: Don't make a function virtual without reason C.132:不要没有理由就将函数声明为虚函数 Reason(原因) Redundant v ...

  5. blob转file对象_C++核心准则C.41:构造函数生成的对象应该被完全初始化

    C.41: A constructor should create a fully initialized object C.41构造函数生成的对象应该被完全初始化 Reason(原因) A cons ...

  6. 判断exception类型_C++核心准则T.44:使用函数模板推断类模板参数类型(如果可能)...

    T.44: Use function templates to deduce class template argument types (where feasible) T.44:使用函数模板推断类 ...

  7. 无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好

    ES.107: Don't use unsigned for subscripts, prefer gsl::index ES.107:不要使用无符号数下标,使用gsl::index更好 Reason ...

  8. unique函数_C++核心准则C.35:基类的析构函数必须满足的条件

    C.35: A base class destructor should be either public and virtual, or protected and nonvirtual 基类的析构 ...

  9. template标签_C++核心准则T.65:使用标签分发提供函数的不同实现

    T.65: Use tag dispatch to provide alternative implementations of a function T.65:使用标签分发提供函数的不同实现 Rea ...

最新文章

  1. RDIFramework.NET ━ 9.5 组织机构管理 ━ Web部分
  2. 阅读php程序设计笔记
  3. 单链表进阶学习 三段
  4. 天池 在线编程 扫雷(BFS)
  5. Oracle11g数据库安装
  6. 【python】 类、对象的练习题
  7. asp.net学习之GridView
  8. 数据结构实验之数组一:矩阵转置
  9. javascript中的array对象属性及方法
  10. [转载] python中 堆heapq以及 队列queue的使用
  11. 发布阿里云OSS for phpcmsV9整合教程
  12. 天锐绿盾报税软件 银行软件 财务软件报错病毒解决
  13. 多语言国家与缩写映射表
  14. Twaver-HTML5基础学习(24)快速查找(QuickFinder)
  15. 电商供应链系统的DDD架构设计实战
  16. 15分钟带你入门Pandas
  17. 南通万豪酒店开业;诺瓦瓦克斯任命新任总裁兼CEO | 美通企业日报
  18. Open Judge 百练 1003 宿醉(C语言)
  19. 无为无欲、与世无争,也就没有烦恼......
  20. css清除浮动带来的负面影响

热门文章

  1. Hibernate中pojo状态
  2. HTML5中引入JS
  3. jQuery的过滤遍历
  4. 【二叉查找树BST】二叉查找树的基本操作总结
  5. 【JavaSE05】Java中方法与重载、递归-练习
  6. java责任链模式做优惠_Java设计模式菜鸟系列(十八)责任链模式建模与实现
  7. sevlet实现下载文件功能
  8. JSunpack-n的安装与简单使用
  9. 数据结构 递归讲解
  10. spring定时器(@Scheduled)