Consider such code:What will happen if realloc() moves the data?Qt Code:
QVector<int> v; ... v.append( v[0] );To copy to clipboard, switch view to plain text mode
Consider such code:What will happen if realloc() moves the data?Qt Code:
QVector<int> v; ... v.append( v[0] );To copy to clipboard, switch view to plain text mode
So why pass t by reference in the first place then?
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
Thanks for the replies guys! jacek you are right! I did not notice that such a thing could happen in the first place! I 've just noticed that the same code exists in STL <vector> as well and there is an enlightening comment:
Qt Code:
_Ty _Tmp = _Val; // in case _Val is in sequenceTo copy to clipboard, switch view to plain text mode
Thanks again!
Hm. Perhaps it is to ensure that the type has a public copy constructor? I'm not sure if a pass-by-value parameter would ensure the same thing.
"The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry
I think that pass-by-value parameter does exactly the same thing i.e it calls the copy constructor to create a copy of the passed object.
I think that an optimazation would be to create the copy only when resize (i.e call realloc) is needed. This would possibly result in a bit larger code but it would probably be better not to needlessly invoke the copy constructor in case that large objects are stored inside the QVector (for example images)
Bookmarks