{
mo = otherDate.mo;
day = otherDate.day;
yr = otherDate.yr;
delete [] msg; // Deallocaate the
// original array
msg = new char[strlen(otherDate.msg) + 1]; // Allocate a new
// array
strcpy(msg, otherDate.msg); // Copy the chars
}
First, the function copies the month, day, and year from the otherDate object into the current object. Next, the function deallocates the current object's dynamic array from the free store, allocates a new dynamic array, and copies all elements of otherDate's array into the new array. The result is therefore a deep copytwo identical class objects pointing to two identical (but separate) dynamic arrays. Given our datel and date2 objects of Figure 17-9, the statement
datel.CopyFrom(date2);
yields the result shown in Figure 17-12. Compare this figure with the shallow copy pictured in Figure 17-11.
Shallow Copy An operation that copies one class object to another without copying any pointed-to data.
Deep Copy An operation that not only copies one class object to another but also makes copies of any pointed-to data.