C++Tutorial for Beginners 45 - Copy Constructors; Creating Copies of Objects

Being able to copy an object is vital in C++. There are two ways to do this; one is via an "overloaded" assignment (=) operator, covered in my advanced course. Another is to provide a special constructor called a copy constructor. Doing so will allow your objects to be passed to functions "by value" (without using references or pointers, although this is a little inefficient), returned from functions (often necessary and useful) and even allows you to write things like Myclass c1 = c2;, where c2 is another instance of "Myclass". Fortunately C++ provided an implicit implementation of the copy constructor, but if you allocate memory in your object, you're going to have to provide your own.



Click here to download source code for this tutorial.