< previous page page_997 next page >

Page 997
{
    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.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Shallow Copy An operation that copies one class object to another without copying any pointed-to data.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Deep Copy An operation that not only copies one class object to another but also makes copies of any pointed-to data.
0997-01.gif
Figure 17-12
A Deep Copy

 
< previous page page_997 next page >