CHAPTER 21

image

Custom Conversions

Custom type conversions can be defined to allow an object to be constructed from or converted to another type. In the following example, there is a class called MyNum  with a single integer field. With conversion constructors it is possible to allow integer types to be implicitly converted to this object’s type.

class MyNum
{
  public:
    int value;
};

Implicit Conversion Constructor

For this type conversion to work, a constructor needs to be added that takes a single parameter of the desired type, in this case an int.

class MyNum
{
  public:
    int value;
    MyNum(int i) { value = i; }
};

When an integer is assigned to an object of MyNum this constructor will implicitly be called to perform the type conversion.

MyNum A = 5; // implicit conversion

This means that any constructor that takes exactly one argument can be used both for constructing objects and for performing implicit type conversions to that object type.

MyNum B = MyNum(5); // object construction
MyNum C(5);         // object construction

These conversions will work not only for the specific parameter type, but also for any type that can be implicitly converted to it. For example, a char can be implicitly converted to an int and can therefore be implicitly changed into a MyNum object as well.

MyNum D = 'H'; // implicit conversion (char->int->MyNum)

Explicit Conversion Constructor

To help prevent potentially unintended object type conversions it is possible to disable the second use of the single parameter constructor. The explicit constructor modifier is then applied, which specifies that the constructor may only be used for object construction, and not for type conversion.

class MyNum
{
  public:
    int value;
    explicit MyNum(int i) { value = i; }
};

The explicit constructor syntax must therefore be used to create a new object.

MyNum A = 5;        // error
MyNum B(5);         // allowed
MyNum C = MyNum(5); // allowed

Conversion Operators

Custom conversion operators allow conversions to be specified in the other direction: from the object’s type to another type. The operator keyword is then used, followed by the target type, a set of parentheses, and a method body. The body returns a value of the target type, in this case int.

class MyNum
{
  public:
    int value;
    operator int() { return value; }
};

When objects of this class are evaluated in an int context, this conversion operator will be called to perform the type conversion.

MyNum A { 5 };
int i = A; // 5

Explicit Conversion Operators

The C++11 standard added explicit conversion operators to the language. Similar to explicit constructors, the inclusion of the explicit keyword prevents the conversion operator from being implicitly called.

class True
{
  explicit operator bool() const {
    return true;
  }
};

The class above provides a safe bool that prevents its objects from mistakenly being used in a mathematical context through the bool conversion operator. In the example below, the first comparison results in a compile error since the bool conversion operator cannot be implicitly called. The second comparison is allowed because the conversion operator is explicitly called through the type cast.

True a, b;
if (a == b) {}             // error
if ((bool)a == (bool)b) {} // allowed

Bear in mind that contexts requiring a bool value, such as the condition for an if statement, counts as explicit conversions.

if (a) {} // allowed
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.89.2