Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should try not to use <code>new</code>, to begin with, as using it brings the trouble of memory management.</p> <p>For your example, just do the following:</p> <pre><code>int main(int, char*[]) { SomeObject myObject; // two phases ParamClass foo(...); myObject.myMethod(foo); // one phase myObject.myMethod(ParamClass(...)); return 0; } </code></pre> <p>I recommend the first method (in two times) because there are subtle gotchas with the second.</p> <p><strong>EDIT</strong>: comments are not really appropriate to describe the gotchas I was referring to.</p> <p>As <code>@Fred Nurk</code> cited, the standard says a few things about the lifetime of temporaries:</p> <p><strong>[class.temporary]</strong></p> <blockquote> <p>(3) Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception. The value computations and side effects of destroying a temporary object are associated only with the full-expression, not with any specific subexpression.</p> <p>(5) The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference [note: except in a number of cases...]</p> <p>(5) [such as...] A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.</p> </blockquote> <p>This can lead to two subtle bugs, that most compilers do not catch:</p> <pre><code>Type const&amp; bound_bug() { Type const&amp; t = Type(); // binds Type() to t, lifetime extended to that of t return t; } // t is destroyed, we've returned a reference to an object that does not exist Type const&amp; forwarder(Type const&amp; t) { return t; } void full_expression_bug() { T const&amp; screwed = forwarder(T()); // T() lifetime ends with `;` screwed.method(); // we are using a reference to ???? } </code></pre> <p>Argyrios patched up Clang at my request so that it detects the first case (and a few more actually that I had not initially thought of). However the second can be very difficult to evaluate if the implementation of <code>forwarder</code> is not inline.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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