Converting between Types

Most programs process information of many types. Sometimes all the operations "stay within a type." For example, adding an int to an int produces an int (as long as the result is not too large to be represented as an int). It is often necessary, however, to convert data of one type to data of another type. This can happen in assignments, in calculations, in passing values to functions and in returning values from functions. The compiler knows how to perform certain conversions among fundamental types (as we discussed in Chapter 6). Programmers can use cast operators to force conversions among fundamental types.

But what about user-defined types? The compiler cannot know in advance how to convert among user-defined types, and between user-defined types and fundamental types, so the programmer must specify how to do this. Such conversions can be performed with conversion constructorssingle-argument constructors that turn objects of other types (including fundamental types) into objects of a particular class. In Section 11.10, we use a conversion constructor to convert ordinary char * strings into String class objects.

A conversion operator (also called a cast operator) can be used to convert an object of one class into an object of another class or into an object of a fundamental type. Such a conversion operator must be a non-static member function. The function prototype

 A::operator char *() const;

declares an overloaded cast operator function for converting an object of user-defined type A into a temporary char * object. The operator function is declared const because it does not modify the original object. An overloaded cast operator function does not specify a return typethe return type is the type to which the object is being converted. If s is a class object, when the compiler sees the expression static_cast< char * >( s ), the compiler generates the call

 s.operator char *()

The operand s is the class object s for which the member function operator char * is being invoked.

Overloaded cast operator functions can be defined to convert objects of user-defined types into fundamental types or into objects of other user-defined types. The prototypes

 A::operator int() const;
 A::operator OtherClass() const;

declare overloaded cast operator functions that can convert an object of user-defined type A into an integer or into an object of user-defined type OtherClass, respectively.

One of the nice features of cast operators and conversion constructors is that, when necessary, the compiler can call these functions implicitly to create temporary objects. For example, if an object s of a user-defined String class appears in a program at a location where an ordinary char * is expected, such as

 cout << s;

the compiler can call the overloaded cast-operator function operator char * to convert the object into a char * and use the resulting char * in the expression. With this cast operator provided for our String class, the stream insertion operator does not have to be overloaded to output a String using cout.

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Simiral book on Amazon © 2008-2017.
If you may any questions please contact us: