C++核心准则C.47:按照成员变量声明的次序定义和初始化数据成员
发布日期:2021-07-01 05:26:33 浏览次数:3 分类:技术文章

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

C.47: Define and initialize member variables in the order of member declaration

C.47:按照成员变量声明的次序定义和初始化数据成员

 

Reason(原因)

To minimize confusion and errors. That is the order in which the initialization happens (independent of the order of member initializers).

最大限度降低混淆和错误。这(声明的次序,译者注)是初始化执行的顺序(和成员初始化器的顺序无关)

 

Example, bad(反面示例)

class Foo {    int m1;    int m2;public:    Foo(int x) :m2{x}, m1{++x} { }   // BAD: misleading initializer order    // ...};Foo x(1); // surprise: x.m1 == x.m2 == 2

 

Enforcement(实施建议)

(Simple) A member initializer list should mention the members in the same order they are declared.

(简单)成员初始化列表应该按照成员被声明的次序处理它们。

 

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c47-define-and-initialize-member-variables-in-the-order-of-member-declaration


 

觉得本文有帮助?欢迎点赞并分享给更多的人。

阅读更多更新文章,请关注微信公众号【面向对象思考】

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

上一篇:C++核心准则C.48:如果构造函数需要用常数初始化成员,使用类内初始化器更合适
下一篇:C++核心准则C.46:默认状态下明确定义单参数构造函数

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月29日 06时27分39秒