when should i use " move constructor"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

when should i use " move constructor"?

8th Mar 2017, 11:32 PM
Navid Tak
Navid Tak - avatar
2 Answers
+ 1
You would use a move constructor when the object you are moving from will not be reused and so you can assume that it can hold invalid data which can lead to great performance gains. Something like a vector is much faster to move from as you can just assign a pointer while a copy would entail a complete copy of the array contents. A move constructor is inherently an optimization. I suggest a read on all the different kinds of references, it's quite interesting.
8th Mar 2017, 11:54 PM
Alexandre Sabourin
Alexandre Sabourin - avatar
+ 1
thank you, Alexandre , but I just want mention this out , a move constructor does shallow copy and that's all, simple and funny funny because we always running away from unsafe data !! love ya Stroustrup :||||||||||
10th May 2017, 3:48 PM
Navid Tak
Navid Tak - avatar