Motivation and use of move constructors in C++

Posted by Giorgio on Programmers See other posts from Programmers or by Giorgio
Published on 2012-05-31T18:29:12Z Indexed on 2012/05/31 22:50 UTC
Read the original article Hit count: 281

Filed under:
|

I recently have been reading about move constructors in C++ (see e.g. here) and I am trying to understand how they work and when I should use them.

As far as I understand, a move constructor is used to alleviate the performance problems caused by copying large objects. The wikipedia page says: "A chronic performance problem with C++03 is the costly and unnecessary deep copies that can happen implicitly when objects are passed by value."

I normally address such situations

  • by passing the objects by reference, or
  • by using smart pointers (e.g. boost::shared_ptr) to pass around the object (the smart pointers get copied instead of the object).

What are the situations in which the above two techniques are not sufficient and using a move constructor is more convenient?

© Programmers or respective owner

Related posts about c++

Related posts about programming-practices