In This Chapter
Introducing the copy/move constructor
Having copies made for you automatically
Creating shallow copies versus deep copies
Avoiding all those copies with a move constructor
The constructor is a special function that C++ invokes automatically when an object is created to allow the object to initialize itself. Chapter 15 introduces the concept of the constructor, whereas Chapter 16 describes other types of constructors. This chapter examines two particular variations of the constructor known as the copy and move constructors.
A copy constructor is the constructor that C++ uses to make copies of objects. It carries the name X::X(const X&), where X is the name of the class. That is, it’s the constructor of class X, which takes as its argument a reference to an object of class X. Now, I know that this sounds really useless, but just give me a chance to explain why C++ needs such beasties.