Why there is a need of move constructor or move semantics in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why there is a need of move constructor or move semantics in C++?

Why creator thinks that move constructor is powerful than implementing copy constructor or copy assignment operator function?

17th Jan 2020, 1:23 PM
Amit chavare
Amit chavare - avatar
1 Answer
+ 1
Look at it as an optimization. The copy generates data duplication and memory reservation. If the original object is going to be unnecessary (because after the operation is ruined), the movement builder is faster because you move pointers instead of data. In the assignment you modify the object instead of using an existing one but you must clean the internal memory before moving pointers Miralo como una optimización. La copia genera duplicación de datos y reserva de memoria. Si el objeto original va a ser imnecesario ( pues luego de la operación se arruina), el constructor de movimiento es mas rápido pues mueves punteros en lugar de datos. En la asignación modificas el objeto en lugar de usar uno ya existente pero debes limpiar la memoria interna antes de mover punteros
24th Jan 2020, 11:20 PM
Master Forts 🇦🇷
Master Forts 🇦🇷 - avatar