本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!