Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your intention is to create a temporary object, but this object <em>would</em> also be <strong>anonymous</strong> : you have no way to refer to it after the semicolon. There is no point in creating an anonymous temporary object just to discard it. You have to instantiate your temporary directly at the call site of the ctor/method that will consume it.</p> <h2>Solution</h2> <p>To pass an anonymous temporary object to a function, you actually need to instantiate it inside the arguments' list :</p> <pre><code>functionExpectingA(A(number)); </code></pre> <p>For the line of "c declaration", you are poking at the <a href="http://en.wikipedia.org/wiki/Most_vexing_parse" rel="nofollow noreferrer"><strong>most vexing parse</strong></a>. If you actually need to pass a default constructed <code>A</code> object as an argument to another object constructor, you need to add another pair of braces to do the trick (so the compiler can distinguish it from a function declaration) :</p> <pre><code>class OtherClass { public: OtherClass(A a) { //... }; }; OtherClass obj((A())); ^ ^ </code></pre> <p><em>EDIT #1</em> : <strong>jrok</strong> pointed out that the argument given to <em>A</em> constructor is not enough to resolve the ambiguity.</p> <p>If you need to pass an anonymous temporary object that is built with an argument, <strike>there is no ambiguity (so no need for the extra parentheses)</strike>, you still need the parentheses around the anonymous object construction.:</p> <pre><code>OtherClass obj((A(number))); </code></pre> <h2>C heritage : a single argument is not enough</h2> <p><em>EDIT #2</em> : "why giving a <strong>single</strong> argument to A constructor does not resolve the ambiguity".</p> <p>What happens with <code>OtherClass obj(A(number));</code> ?<br/> This is a declaration for a function named <code>obj</code>, taking an <code>A</code> object as its unique argument. This argument is named number.<br/> i.e: It is exactly equivalent to <code>OtherClass obj(A number);</code>. Of course, you can omit the argument name at function declaration. So it is also sementically equivalent to <code>OtherClass obj(A);</code></p> <p><a href="https://stackoverflow.com/questions/7007817/a-confusing-detail-about-the-most-vexing-parse">The syntax with parentheses around the object name is inherited from C</a> :</p> <pre><code>int(a); // declares (and defines) a as an int. int a; // strictly equivalent. </code></pre> <h2>What with more arguments to the constructor then ?</h2> <p>If you added a ctor to <code>OtherClass</code> taking two (or more) arguments :</p> <pre><code> OtherClass(A a1, A a2) { //... }; </code></pre> <p>Then this syntax :</p> <pre><code>A arg1, arg2; OtherClass obj(A(arg1, arg2)); </code></pre> <p>Would this time actually declare <code>obj</code> as an instance of <code>OtherClass</code>.<br/> This is because <code>A(arg1, arg2)</code> cannot be interpreted as name declaration for an <code>A</code>. So it is actually parsed as the construction of an anonymous <code>A</code> object, using the constructor with 2 parameters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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