Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This may be a bit off topic, but there are differences between G++ (MinGW 4.4.1) and MSCV++ 10 in the scope of returned temporary variables created on the stack.</p> <p>I just created an object, called say myObj and class members are coordinates, vectors, whatever ...</p> <p>The class has a default constructor, another constructor that initializes class members, a copy constructor, and overloaded +, += and = operators.</p> <pre><code>myObj &amp; myObj::operator+(myObj &amp; mO) { myObj retO = myObj(mO); // using a non-default constructor defined retO += *this; // Uses the other overloaded operator defined. return retO; } </code></pre> <p>The + operator allocates a myObj object on the stack (NOT using new) and adds the objects *this and mO (via retO) and assigns them to the retO object created on the stack.</p> <p>G++ allows you to call:</p> <pre><code>myObj2 = myObj1 + myObj2; </code></pre> <p>However, Visual Studio 10.0 destroys retO that was created on the stack immediately after the overloaded operator member function exits (verified using static member increment and decrement with output inside all constructors and the destructor).</p> <p>I do not know how the standard defines this behavior. Oddly enough, MinGW gives a compiler warning whereas MSVC++ 10 does not.</p> <p>Of course the problem with creating a new myObj object with the new keyword is memory leakage as the above construct does not keep a reference to the new myObj instantiation, which is allocated to myObj2.</p> <p>Regards,</p> <p>Navid</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. 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