c++ - Why post-increment needs to make a copy while pre-increment does not -
i know issue has been discussed several times , not find post explains why copy needs made in case of post-increment operation.
quoting stackoverflow reply:
int j = i++; // j contain i, incremented. int j = ++i; // incremented, , j contain i+1.
which makes sense when definition of post/pre increment considered. many times when comparing performance of pre/post increment said post increment needs make copy, increment , return copy while pre-increment increases value , not create copy.
although performance has been compared in tens of posts, not find explanation on why copy has made in case of post-increment. why doesn't return old value , increments value of variable one(or way operator overloaded), rather creating new object , return one.
the difference someval++
returns someval
before increment , need remember someval
copy. how else return original value while updating if original value wasn't stored somewhere?
Comments
Post a Comment