Ad Hoc Polymorphism  «Prev  Next»
Lesson 6 ADT conversions
Objective Comparing conversion operators and constructors

Conversion Operators and Constructors in C++

Understand when to use conversion operators and when to use constructors to perform conversion.
Here is a table that explains the differences between using a constructor for conversions and using a special conversion function.

Conversion Constructor Conversion Operator
used to convert from built-in types to user-defined types used to convert from user-defined to built-in types
always has only one "from-type" argument, while the "to-type" argument is implied has no parameters
creates new objects of user-defined type has no return type; creates built-in type object
available for both implicit and explicit conversions (it is used explicitly in either cast or functional form) conversions occur implicitly in assignment expressions, in arguments to functions, and in values returned from functions

Objects, Abstraction, Data Structures

Conversion Operators

A conversion operator[1] is a special kind of member function that converts a value of a class type to a value of some other type. A conversion function typically has the general form
operator type() const;

where type represents a type. Conversion operators can be defined for any type (other than void) that can be a function return type. Conversions to an array or a function type are not permitted. Conversions to pointer types (both data and function pointers) and to reference types are allowed.
Conversion operators have no explicitly stated return type and no parameters, and they must be defined as member functions. Conversion operations ordinarily should not change the object they are converting. As a result, conversion operators usually should be defined as const members.
Note: A conversion function must be a member function, may not specify a return type, and must have an empty parameter list. The function usually should be const.

Defining Class with Conversion Operator

We will define a small class that represents an integer in the range of 0 to 255:
class SmallInt {
SmallInt(int i = 0): val(i)
 if (i < 0 || i > 255)
 throw std::out_of_range("Bad SmallInt value");
 operator int() const { return val; }
 std::size_t val;

Our SmallInt class defines conversions to and from its type. The constructor converts values of arithmetic type to a SmallInt. The conversion operator converts SmallInt objects to int:
SmallInt si;
si = 4; // implicitly converts 4 to SmallInt then calls SmallInt::operator=
si + 3; // implicitly converts si to int followed by integer addition

Although the compiler will apply only one user-defined conversion at a time , an implicit user-defined conversion can be preceded or followed by a standard (built-in) conversion. As a result, we can pass any arithmetic type to the SmallInt constructor. Similarly, we can use the converion operator to convert a SmallInt to an int and then convert the resulting int value to another arithmetic type:
// the double argument is converted to int using the built-in conversion
SmallInt si = 3.14; // calls the SmallInt(int) constructor
// the SmallInt conversion operator converts si to int;
si + 3.14; // that int is converted to double using the built-in conversion

Because conversion operators are implicitly applied, there is no way to pass arguments to these functions. Hence, conversion operators may not be defined to take parameters. Although a conversion function does not specify a return type, each conversion function must return a value of its corresponding type:
class SmallInt;
operator int(SmallInt&); // error: nonmember
class SmallInt {
  int operator int() const; // error: return type
  operator int(int = 0) const; // error: parameter list
  operator int*() const { return 42; } // error: 42 is not a pointer
Please note: A
  1. conversion member function of the form A::operator B() and
  2. a constructor of the form B::B(const A&)
both provide conversions from type A objects to type B objects. Having both in your class can result in ambiguity errors.

ADT Conversions Constructor - Quiz

Click the Quiz link below to take a short multiple-choice quiz on ADT conversions.
ADT Conversions Constructor - Quiz

[1]conversion operator: A member function that defines a conversion from the class type to another type. A conversion operator must be a member of the class from which it converts and is usually a const member. These operators have no return type and take no parameters. They return a value convertible to the type of the conversion operator. That is, operator int returns an int, operator string returns a string.