< previous page page_1001 next page >

Page 1001
In summary, the default operations of assignment and initialization may be dangerous when class objects point to dynamic data on the free store. Member-by-member assignment and initialization cause only pointers to be copied, not the pointed-to data. If a class allocates and deallocates data on the free store, it almost certainly needs the following suite of member functions to ensure deep copying of dynamic data:
class SomeClass
{
public:
      .
      .
      .
    void CopyFrom( SomeClass anotherObject );
        //A deep copy operation

    SomeClass(  );
        // Constructor, to create data on the free store

    SomeClass( const SomeClass& anotherObject );
        // Copy-constructor, for deep copying in initializations

    ~SomeClass();
        // Destructor, to clean up the free store
private:
    .
    .
    .
};
At the beginning of this chapter, we said that pointers are used for two reasons: to make a program more efficienteither in speed or in memory usageand to create complex data structures called linked structures. We give examples of the use of pointers to make a program more efficient in the case studies in this chapter. Linked structures are covered in Chapter 18.
Problem-Solving Case Study Personnel Records
1001-01.gif
Problem: We have a file of personnel records, and there is a great deal of data associated with each person. The task is to read in these records, sort them alphabetically by last name, and print out the sorted records.
Input: A file of personnel records (masterFile), where each record corresponds to the data type PersonnelData in the declarations below.

 
< previous page page_1001 next page >