Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why explicit initialization list is more likely to failure?

Tags:

c++

In the book "Inside the C++ object model", the author says that:

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.

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.

like image 821
Wizmann Avatar asked Jun 06 '14 13:06

Wizmann


1 Answers

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
like image 199
Roddy Avatar answered Nov 12 '22 17:11

Roddy