c – 为什么显式初始化列表更有可能失败?

在“Inside the C object model”一书中,作者说:

There are three drawbacks of an explicit initialization list:

  1. It can be used only if all the class members are public.

  2. 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.

我不知道为什么显式初始化列表更容易失败. “编译器自动应用”的含义是什么?

是否有一些例子来证明这一观点.

感谢您的回答.

解决方法

以下是Lipmann书中显式初始化列表的示例.
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);
 Point local4; // uses default values of 0,0

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...