deconstructor called after constructor

This topic is 3760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hello, i want to reuse a variable for another instance

myClass foo = myClass();

// some code here

foo = myClass();

// some other code here


the problem is, that it seems as if in the second line the constructor is called first, and then the destructor is automatically called on the new instance. i expected it to call the destructor first on the old instance and then call the constructor on the new instance. why exactly is the destructor called on the second instance here? thanks.

Share on other sites
The second line creates a temporary object and then uses the assignment operator on the first object to copy the state of the temporary to the original.

Share on other sites
ok, but why is the destructor called on the new object then?

Share on other sites
Because once it gets assigned to the old object there's no reason to keep it around anymore.

Share on other sites
thank you,

but the way i understand it, there is no reason to keep the OLD object (the one that was created in the first line) around anymore. because the new object is now stored in foo, it should be kept around.

Share on other sites
Let's try explaining it this way, when you do this:

int i = 5;
i = 4;

i isn't destroyed. It gets a new value, but it's the same variable. The same thing happens with with class type. If you have

Foo f;
f = myClass();

f isn't destroyed. It just gets a new value which is equal to that of a default constructed myClass. The default constructed myClass temporary lives just long enough to pass its value on to f.

Share on other sites
Quote:
 Original post by ehmdjiithank you,but the way i understand it, there is no reason to keep the OLD object (the one that was created in the first line) around anymore. because the new object is now stored in foo, it should be kept around.
New object yes, temporary, no.

// This:myClass foo = myClass();// Is the same as:myClass tmp;myClass foo(tmp); // I think, not 100% sure; might just use operator= rather than copy constructor// destroy tmp here, it's no longer needed

After that, foo contains whatever tmp contained, and tmp doesn't need kept around any more.

Likewise:
// This:foo = myClass();// Is the same as:myClass tmp2;foo = tmp2;// Destroy tmp2 here

Share on other sites
Quote:
 Original post by Evil StevemyClass foo(tmp); // I think, not 100% sure; might just use operator= rather than copy constructor// destroy tmp here, it's no longer needed
Sometimes the copy-constructor is called instead of the assignment operator, but never the other way around.
Both examples call the copy-constructor.

Share on other sites
Quote:
Original post by iMalc
Quote:
 Original post by Evil StevemyClass foo(tmp); // I think, not 100% sure; might just use operator= rather than copy constructor// destroy tmp here, it's no longer needed
Sometimes the copy-constructor is called instead of the assignment operator, but never the other way around.
Both examples call the copy-constructor.

The copy constructor is never called instead of the assignment operator. If you're assigning to an existing object, calling the copy constructor instead would double construct an object. If you're "assingning" to an non-constructed object, the assignment operator wouldn't be called in the first place.

Share on other sites
ok thank you everybody.

so you are saying that the NEW object's contents are copied over to the OLD object. which means that only the values in the OLD object change, but the object stays the same.

the reason why i am actually posting this questions is because in the constructor some array is allocated which is deleted in the destructor. and if i call a function that accesses this array, the program crashes, because the array seems to got deleted.

myClass foo = myClass();foo.operateOnArray(); // worksfoo = myClass();foo.operateOnArray(); // <- crash because the destructor has deleted the array?

1. 1
2. 2
Rutin
26
3. 3
4. 4
JoeJ
18
5. 5
gaxio
11

• 14
• 22
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631763
• Total Posts
3002196
×