网站首页 > 基础教程 正文
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
觉得本文有帮助?请分享给更多人。
更多精彩文章请关注微信公众号【面向对象思考】!
面向对象开发,面向对象思考!
猜你喜欢
- 2024-10-10 C++系列1-1:初探C++ c=2μf
- 2024-10-10 浅谈C++11(第9篇 可变参数模板) c++可变参数模板类
- 2024-10-10 数组的初始化方式有哪几种? 数组的初始化是什么意思
- 2024-10-10 c++对于内建类型的默认初始化 创建内部类的对象
- 2024-10-10 C++类构造函数,如何初始化对象?linux C++第27讲
- 2024-10-10 【C++编程语言】之 类和对象——对象的初始和消除
- 2024-10-10 C++基础语法梳理:引用、封装、继承和多态
- 2024-10-10 C++核心准则?:按照成员声明的顺序定义和初始化成员变量
- 2024-10-10 C/C++语言编程系列002——不同情况下数组的初始化方法
- 2024-10-10 c++面试常见问题汇总—建议收藏 c++面试题总结
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)