Move semantics

Hi guys there seems to be a debate on stackoberflow on why you should leave a rvalues data members in a certain state even thought you will never use it again

https://stackoverflow.com/questions/3106110/what-are-move-semantics

^^ first answer

so why would you have to set the lvalue objects members to null or 0 when it is just going to be destructed anyway?

as one user says

But if my ctor is getting an rvalue, which can never be used later, why do I even need to bother leaving it in a consistent/safe state? Instead of setting that.data = 0, why not just leave it be?


then another user answers with this
Because without that.data = 0, the characters would be destroyed way too early (when the temporary dies), and also twice. You want to steal the data, not share it!


what does he mean by destroyed too early and how would this affect anything?


thanks
why would you have to set the lvalue objects members to null or 0 when it is just going to be destructed anyway?

You answered your own question. "it is ... going to be destructed", i.e., it's dtor will be called, and you don't want the dtor to delete the array you just grabbed! Instead, you need the dtor to see an empty array and do nothing.
Last edited on
good point because they will both point to the same data

thanks :)
Topic archived. No new replies allowed.