作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在《Inside the C++ object model》一书中,作者说:
There are three drawbacks of an explicit initialization list:
It can be used only if all the class members are public.
It can specify only constant expressions (those able to be evaluated at compile time).
3. Because it is not applied automatically by the compiler, the likelihood of failure to initialize an object is significantly heightened.
不知道为什么显式初始化列表更容易失败。 “由编译器自动应用”是什么意思?
有没有例子可以证明这个观点。
感谢您的回答。
最佳答案
这是李普曼书中显式初始化列表的示例。
Point1 local1 = { 1.0, 1.0, 1.0 };
我认为他试图表达的意思是您必须记住使用显式初始化!换句话说,它们不能替代构造函数。如果您忘记使用列表...
Point local2;
...那么您“无法初始化对象”。并不是初始化列表会以任何方式失败,只是您可能忘记使用它。
与构造函数比较
Point::Point (int x=0, int y=0, int z=0) : x(x), y(y) z(z) {};
您现在可以同时执行这两项操作,并且仍能获得明确定义的结果。
Point local3(1.0, 1.0, 1.0);
Point local4; // uses default values of 0,0,0
关于c++ - 为什么显式初始化列表更容易失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24083379/
我是一名优秀的程序员,十分优秀!