The copy constructor is a constructor that takes a single paramter that is:
1. Usually const
2. A reference to an object of the same type. (Meaning the class that is being defined.)
The copy constructor is important in C++ for a number of reasons:
- It is used to initialize an object from another object of the same type. Everything other use of the copy constructor stems from this use.
- The copy constructor is used to copy an object to pass it as an argument to a function. That is, when an object is passed by value, the copy constructor is called.
- And of course, it is used to copy an object to return it from a function.
- It is also used initialize elements of a sequential container
The important thing to note is that if we don't define a copy constructor, the compiler will define one for us. The new object is initialized by copying the members of the old object one by one. For some cases this is actually what we want.