push_back method in STL vector class has const T & argument. So argument is passed via reference. This logic can provide to some kind of bug. Look at this code: std::vector ... if (v.size()) v.push_back(v[0]); // Add first element also as last element As you can see we insert first element after the last. But, if vector is reallocating while inserting it can provide to invalidate variable (because it passed via reference). But fortunately it is not true. It will be ridicoulus to leave this kind of behaviour inside STL vector. So push_back method invokes _Insert_n method, where is this line of code: _Ty _Tmp = _Val; // in case _Val is in sequence This makes copy (copy constructor invoked) of inserted variable to prevent this kind of situation (in exchange for slower inserting). |
czwartek, 9 czerwca 2011
programming gem
Subskrybuj:
Komentarze do posta (Atom)
Brak komentarzy:
Prześlij komentarz