Re: mysterious destructors
On 2/17/2015 6:59 PM, Stefan Ram wrote:
Christopher Pisz <nospam@notanaddress.com> writes:
On 2/17/2015 6:22 PM, Stefan Ram wrote:
int main()
{ c o = * new c( 1 );
o.print();
o = * new c( 2 ); /* overwrite */
o.print(); }
By assigning another instance to the pointer you lost the first. Raw
But both ?o? and ?* new c( 2 )? are not pointers, but rather
an object and a temporary, respectively, as far as I understand it.
?new c( 2 )? is a pointer, but ?*? makes a temporary from
the pointer, as far as I understand it.
So, the assignment ?o = * new c( 2 )? has an object on the
left and a temporary on the right.
True, kind of. the <new> returns a pointer. You de-referenced it, and
assigned it to an object on the stack, whom was already default
constructed, but you saw no message for that or the assignment because
you didn't implement the default constructor or the assignment operator
yourself.
So really, you have 3 instances and the message is erroneous, because it
is actually the instance on the stack getting destroyed, while both on
the heap are leaked.
Nay my friend. You have demonstrated a memory leak.
(I am aware that the ?new? creates a memory leak missing the
corresponding ?delete?, but this was not my primary concern,
so - for simplification - I omitted the ?delete?.)
Oh, ok, I didn't realize you did it on purpose.
Implement the default constructor and the assignment operator with
messages and see if it makes sense.