Why explicit initialization list is more likely to failure?
In the book "Inside the C++ object model", the author says that:
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.
I don't know why the explicit initialization list is more likely to failure. And what is the meaning of "applied automatically by the compiler"?
Are there some examples to prove this point of view.
Thanks for your answer.
Here's the example of an explicit initialization list in Lipmann's book.
Point1 local1 = { 1.0, 1.0, 1.0 };
I think the point he's trying to make is that you must remember to use the explicit initialization! In other words they're not a replacement for constructors. If you forget to use the list...
Point local2;
... then you have "failed to initialize the object". It's not that the initialization list can fail in any way, simply that you can fail to remember to use it.
Compare with a constructor
Point::Point (int x=0, int y=0, int z=0) : x(x), y(y) z(z) {};
You can now do both, and still get well defined results.
Point local3(1.0, 1.0, 1.0);
Point local4; // uses default values of 0,0,0
链接地址: http://www.djcxy.com/p/79690.html
上一篇: YCM没有找到我的标题?
下一篇: 为什么显式初始化列表更可能失败?