Jump to content
  • Advertisement
Sign in to follow this  
ehmdjii

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by ehmdjii
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.
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 this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
myClass 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 this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Quote:
Original post by Evil Steve
myClass 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 this post


Link to post
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(); // works

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




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!