Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>temp temp1; </code></pre> <p>This calls <code>temp</code>'s default constructor on the instance called <code>temp1</code>.</p> <pre><code>temp temp2 = temp(); </code></pre> <p>This calls <code>temp</code>'s default constructor on a temporary object, then calls the compiler-generated copy-constructor on <code>temp2</code> with the temporary object as the argument (this of course assumes that the compiler doesn't elide copies; it depends on your compiler's optimization settings).</p> <p>As for why you get different initialized values, section 8.5 of the standard is relevant:</p> <hr> <h2>8.5 Initializers [dcl.init]</h2> <p><h3>Paragraph 5:</h3> To <em>zero-initialize</em> an object of type <code>T</code> means:</p> <ul> <li>if <code>T</code> is a scalar type (3.9), the object is set to the value of 0 (zero) converted to <code>T</code>;</li> <li>if <code>T</code> is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;</li> <li>if <code>T</code> is a union type, the object’s first named data member is zero-initialized;</li> <li>if <code>T</code> is an array type, each element is zero-initialized;</li> <li>if <code>T</code> is a reference type, no initialization is performed.</li> </ul> <p>To <em>default-initialize</em> an object of type <code>T</code> means:</p> <ul> <li>if <code>T</code> is a non-POD class type (clause 9), the default constructor for <code>T</code> is called (and the initialization is ill-formed if <code>T</code> has no accessible default constructor);</li> <li>if <code>T</code> is an array type, each element is default-initialized;</li> <li>otherwise, the object is zero-initialized.</li> </ul> <p>To value-initialize an object of type <code>T</code> means:</p> <ul> <li>if <code>T</code> is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for <code>T</code> is called (and the initialization is ill-formed if <code>T</code> has no accessible default constructor);</li> <li>if <code>T</code> is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;</li> <li>if <code>T</code> is an array type, then each element is value-initialized;</li> <li>otherwise, the object is zero-initialized.</li> </ul> <p><h3>Paragraph 7:</h3> An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.</p> <p><h3>Paragraph 9:</h3> If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.</p> <h2>12 Special Member Functions [special]</h2> <p><h3>Paragraph 7:</h3> An implicitly-declared default constructor for a class is implicitly defined when it is used to create an object of its class type (1.8). The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with an empty mem-initializer-list (12.6.2) and an empty function body.</p> <h2>12.6.2 Initializing bases and members [class.base.init]</h2> <p><h3>Paragraph 4:</h3> If a given nonstatic data member or base class is not named by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then</p> <ul> <li>If the entity is a nonstatic data member of (possibly cv-qualified) class type (or array thereof) or a base class, and the entity class is a non-POD class, the entity is default-initialized (8.5). If the entity is a nonstatic data member of a const-qualified type, the entity class shall have a user-declared default constructor.</li> <li>Otherwise, the entity is not initialized. If the entity is of const-qualified type or reference type, or of a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of a const-qualified type, the program is ill-formed.</li> </ul> <hr> <p>So now that the rules have been laid out, let's see how they apply:</p> <pre><code>temp temp1; </code></pre> <p><code>temp</code> is a non-POD type (because it has a <code>std::string</code> member), and since no initializer is specified for <code>temp1</code>, it will be default-initialized (8.5/9). This calls the default constructor (8.5/5). <code>temp</code> has an implicit default constructor (12/7) which default-initializes the <code>std::string</code> member and the <code>int</code> member isn't initialized at all (12.6.2/4).</p> <pre><code>temp temp2 = temp(); </code></pre> <p>On the other hand, the temporary <code>temp</code> object is value-initialized (8.5/7), which value-initializes all data members (8.5/5), which calls the default constructor in the <code>std::string</code> member and zero-initializes the <code>int</code> member (8.5/5).</p> <p>Of course, if you much rather not have to refer to the standard in 5+ different places, just ensure that you explicitly initialize everything (e.g. <code>int i = 0;</code> or using initializer lists).</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