Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Warning: Speculation follows.</p> <p>C++ classes were based on C structs, with a number of added features. Members of C structures can't have initializers (because it would require code to be executed every time an object of the structure type is created, something that was considered too complex for early C compilers).</p> <p>Early C++ (originally called "C with Classes") added constructors, a mechanism that does require code to be executed every time an object of a given type is created. Since a constructor can do everything that a member initializer could do, member initializers were probably seen as unnecessary.</p> <p>But as you imply in your question, it would be <em>convenient</em> to have member initializers, even if it isn't strictly necessary. And the new 2011 ISO C++ standard has added that feature to the language, so apparently the ISO C++ committee agreed that they're a good idea. This feature increases the complexity of the language (for example, there have to be new rules governing the order in which initializers and constructors are executed, and code that depends on that order will be potentially confusing). The committee decided that the convenience was worth the added complexity. (I <em>think</em> I agree.)</p> <p>(Bjarne Stroustrup, the inventor of the C++ language, has a book, "<a href="http://rads.stackoverflow.com/amzn/click/0201543303" rel="nofollow">The Design and Evolution of C++</a>", that discusses his early design decisions. I haven't (yet) checked it to see whether he mentions this particular issue.)</p> <p>Summary: Member initializers are <em>convenient</em> but not <em>necessary</em>, and it simply took a while to decide to add them as a language feature.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload