
Conversions between Objects and Basic Types
Three types of situations might arise in the data conversion between uncompatible types:
1 Conversion from built-in type to class type.
2 Conversion from class type to built-in type.
3 Conversion from one class type to another class type.
When we want to convert between user-defined data types and basic types, we can't rely on built-in conversion routines, since the compiler doesn't know anything about user-defined types besides what we tell it. Instead, we must write these routines ourselves.
Basic to Class Type
To go from a basic type to a user-defined type, we use a constructor with one argument. These are sometimes called conversion constructors. A single argument constructor is a constructor that may be called with only one argument. Such a constructor may declare a single parameter or it may declare multiple parameters, with each parameter after the first having a default value. Here is one single-argument constructor example.
string : : string ( char *a )
{
length = strlen ( a ) ;
P = new char [ length + 1 ] ;
strcpy ( P , a ) ;
}
This constructor builds a string type object from a char * type variable a. The variables length and P are the data members of the class string. Once this constructor has been defined in the string class, it can be used for conversion from char * type to string type.
string str1, str2 ;
char * name1 = " Computer " ;
char * name2 = " Science" ;
str1 = string ( name1 ) ;
str2 = name2 ;
The statement
str1 = string ( name1 ) ;
first converts name1 from char * type to string type and then assigns the string type values to the object str1. The statement
str2 = name2 ;
also does the same job by invoking the constructor implicitly.
explicit Keyword
While constructors taking one argument are often useful in the design of a class, they can allow inadvertent conversion in expressions. C++ keyword explicit allows the class designer to prohibit such implicit conversions. Keyword explicit is used in the declaration of constructors within class declarations.
class string
{
public :
explicit string ( char *a ) ;
// . . . .
} ;
str1 = string ( name1 ) ; // ok: explicit conversion
str2 = name2 ; // error: no implicit conversion
Class to Basic Type
C++ allows us to define a overloaded casting operator function, usually referred to as a conversion function,
operator typename ( )
{
function statements
}
This function converts a class type data to typename. For example, the operator double ( ) converts a class object to type double, the operator int ( ) converts a class type object to type int, and so on.
Consider the following conversion function:
class Rational
{
public :
. . . . . . .
operator double ( ) const ;
} ;
This function would be automatically invoked in contexts like this:
Rational r ( 1, 2 ) ; // r has the value 1/2
double d = 0.5 * double ( r ) ; // converts r to double
// then does multiplication
or
double d = 0.5 * r ; // same as above
A casting operator function should satisfy the following conditions:
It must be a class member
It must not specify a return type
It must not have any arguments
Conversion Between Objects of Different Classes
The same two methods just shown for conversions between basic types and user defined types also apply to conversions between two user defined types. That is, you can use a one-argument constructor or you can use a conversion operator function. The choice depends on whether you want to put the conversion routine in the class declaration of the source object or of the destination object.
For example, suppose you say
X objectX ;
Y objectY ;
objectX = objectY ;
Y is known as the source class and X is known as the destination class.
When the source object needs to be converted a casting operator function can be used. When the destination object needs to be converted a single argument constructor function can be used.