Is it possible to replace:
是否可以將視頻裏向量擴容代碼中的:
for (int i = 0; i < _size; i++) _elem[i] = oldElem[i];
in the vector expansion code in the video with:
替代爲:
memcpy(_elem, oldElem, _size * sizeof(T));
答案:
No, because whether the latter can achieve the purpose is related to element type T.
否,因爲後者能否達到目的與元素類型T有關。
分析:
When T is a non-base type and there is a corresponding assignment operator to perform deep copy, the previous section of code calls the assignment operator, and the latter section can only perform shallow copy.
當T爲非基本類型且有對應的賦值運算符以執行深複製時,前一段代碼會調用賦值運算符,而後一段只能進行淺複製。