4.10 Assignment Operator


A class can define its own assignment operator, which defines what actions occur when a value is assigned to an object. A class-specific assignment operator is needed because, similar to a copy constructor, special actions may need to be taken to insure that the object behaves in a reasonable way when objects of the same class are assigned to one another. By default, object assignment is bitwise: a bit-by-bit copy is done from the source object (the one on the right-hand side of the assignment operator) to the target object (the one on the left-hand side of the assignment operator).

The Message class illustrates the need for a class-specific assignment operator. The following example shows two Message objects, one of which is assigned to the other:

       Message targetMsg("Hello Word");
        Message sourceMsg("MacroSoft, Inc.");

        ...

        targetMsg = sourceMsg;          // assignment

Before the assignment statement is executed, the structure of the objects is as follows:


Before Assignment Statement

After the default (bitwise) assignment operation is completed the situation is this:


After Assignment Statement

There are two obvious, and serious, problems created by the default assignment. First, a memory leak has been created because the original targetMsg string is no longer accessible but has not been returned to the memory management system. Second, both objects (targetMsg and sourceMsg) point to the same character string. Because they point to the same string, the destruction of one of the Message objects causes the other Message object to point to memory with undefined contents.

What is desired by the assignment operator is the following:

Desired Result of Assignment

In this situation, the original targetMsg string ("Hello World") has been deallocated and the targetMsg now points to a copy of the original sourceMsg string ("MacroSoft, Inc.").

The Message class shown below includes a class-specific assignment operator:

  class Message {
     private:
        char* message;
        ...
     public:
        ...
        void operator=(const Message& source);
        ...
   };


   void Message::operator=(const Message& source) {
        delete message;                        // deallocate previous string
        message = copystring(source.message); // copy new string
   }

The name of the assignment operator is "operator=". The "operator" part is mandatory and signifies that a class-specific definition is being given for a standard C++ operator. Which standard C++ operator is being given a class-specific meaning is determined by the symbol(s) that follow immediately after the "operator" keyword.

 


Tasks

  1. Unsafe Address Class: The Address class represents a postal-system address defined by two character strings denoting the street address, and the city and state, and an integer zip code. All three items are required by the constructor. For example:

     Address office("123 Main St.", "Blacksburg, VA", 24061);

    Address home ("456 Cozy Dr.", "Blacksburg, VA", 24061);

    The Address class has a method SameZip that returns 0 or 1 indicating whether the address object executing the method has the same zip code as the Address object passed as a parameter by copy. For example:

     int same = office.SameZip(home);
    					

    Implement an unsafe version of the Address class that has neither a copy constructor nor an assignment operator. Write a main program that uses this class and show how using the unsafe class can result in run-time errors, unexpected behavior, and/or memory leaks.

  2. Safe Address Class: Implement a safe version of the Address class. This version must be designed so that Address objects may be copied, assigned, and destructed safely and without memory leaks.
  3. Add an assignment operator to the Shape class. Test your code with a simple main program.
  4. Add an assignment operator to the Location class. Test your code with a simple main program.

 




©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement