8.4 Type Conversion Operators


 


Type conversion is the act of producing a representation of some value of a target type from a representation of some value of a source type. Type conversion from int to long, and from int to float are among the familiar base (built-in) types conversions. These type conversions are meaningful because the source value has a natural, useful projection in the universe of values defined by the target type. For example, the integer 2 has a natural conversion to the floating point value 2.0.

Type conversion is needed to resolve mismatched types in assignments and expressions, or when passing parameters. The existence of type conversions makes it possible to use one type when a different type may be expected, the type conversion bridging the difference between them.

Type conversions may be either implicit or explicit, as shown in the following examples using built in types:

       int i;
        float f;

        f = i;          // implicit conversion

        f = (float)i;   // explicit conversion

        f = float(i);   // explicit conversion

With implicit type conversion, the compiler is given the responsibility to determine when a conversion is required and how to perform the conversion. With explicit type conversion, the programmer assumes this responsibility. Notice that there are two different, but equivalent, syntaxes for explicit conversion.

Type conversions involving user-defined classes are also meaningful. The following examples illustrate conversion between a user defined class and a built-in type and conversion between two user defined types.

The first example, illustrating type converstion between a user-defined class and a built-in type, uses the class Counter introduced in chapter 6, on inheritance. Objects of this class can be incremented by a Next operation and displayed in the user interface by associating the Counter with a TextBox. The internal integer value of a Counter can be extracted by the Value method so that a Counter object can be used in contexts where an integer values is expected:

       Counter count(0);

        // manipulate count

        if ( count.Value() > 0) ...

        if ( count.Value() == 100) ...

        cout << count.Value();

This syntax, however, is awkward and unnatural.

Because a Counter object can be viewed as an integer value that has added capabilities (i.e., the ability to be displayed in the user interface), it is sensible to expect that a Counter object could be used in the following, more direct, ways:

       Counter count(0);

        // manipulate count

        if ( count > 0) ...

        if ( count == 100) ...

        cout << count;

In this code, the expectation is that a Counter object can be treated as a simple integer. One way to achieve this effect is to add to the Counter class overloaded operators for such functions as comparisons, arithmetic, and/or stream I/O. A simpler alternative is to use type conversion.

A type-conversion operator is added to the user defined Counter class as in the figure below, where the operator overloading defines a type-conversion operator that can be used to produce an int type from a Counter object. This operator will be used whenever an implicit or explict conversion of a Counter object to an int is required.


Defining a Type Conversion Operator
class Counter 
{
 ...
 public:
   ...
   operator int();
   ...
};

Notice that constructors also play a role in type conversion. The constructor for a Counter object is defined in the Counter class as shown below. This constructor can be viewed as a way of converting an int value to a Counter object. In general, a class's constructor that takes a single argument of a type other than that class itself serves as a type converted from the argument type to the class.


Constructors as Type Conversion Mechanisms
class Counter : public DisplayableNumber 
{
  ...
  public:
     Counter(int i);
     ...
};

 

  1. Revise the Message class to allow correct type conversion from a char* to a Message and from a Message to a char*.

  2. Revise the Number class to allow correct type conversion from a Number to an int and from an int to a Number.

  3. Revise the Number and Counter classes to allow correct type conversion from a Number to a Counter and a Counter to a Number.

  4. Revise the Shape class to allow a Shape object to be type converted correctly to both an int and a float where the int and float values represent the area of the rectangle described by the Shape object.

 




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

Legal Statement