Polymorphism  «Prev  Next»
Lesson 5 ADT conversions
Objective Conversion functions in C++

ADT conversions (C++ member functions)

To convert from a user-defined type to a built-in type in C++, you would typically create a member function that returns the built-in type. This member function can be treated as a conversion function, providing a way to represent your user-defined type in the form of a built-in type.
Here's a basic example of how you might implement this:
Example 1:
class MyType {
private:
    int value;
public:
    // Constructor
    MyType(int val) : value(val) {}

    // Conversion function
    int toInt() const {
        return value;
    }
};

To convert from a user-defined type to a built-in type in C++, you would typically create a member function that returns the built-in type. This member function can be treated as a conversion function, providing a way to represent your user-defined type in the form of a built-in type.
Example 2:
Here's a basic example of how you might implement this:
class MyType {
private:
    int value;
public:
    // Constructor
    MyType(int val) : value(val) {}

    // Conversion function
    int toInt() const {
        return value;
    }
};
In this example, an object of MyType can be converted to an int by calling the toInt() member function.
However, you may sometimes want the conversion to happen implicitly, without having to call a member function. In C++, you can do this with a conversion operator. A conversion operator is a special kind of member function that enables an object to be converted to another type.
Here's an example:
class MyType {
private:
    int value;
public:
    // Constructor
    MyType(int val) : value(val) {}

    // Conversion operator
    operator int() const {
        return value;
    }
};

With this operator int() function, an object of MyType can be implicitly converted to an int. Here's how you might use it:
MyType obj(10);
int val = obj;  // Implicit conversion
It's important to be cautious when using conversion operators, as they can lead to unexpected results if you're not careful. For example, the implicit conversion may be triggered when you didn't intend for it to be, or it may not be triggered when you did. It's often a good idea to make your intentions explicit by calling a named function, as in the toInt() example.
Understand how to create special conversion member functions to convert from a user-defined type to built-in type.
While it is relatively easy to use a constructor to convert from an already-defined type to a user-defined type, it is not possible to add a constructor to a built-in type such as int or double. However, we can define a special conversion function inside the class to achieve the same thing. To do this, we use the keyword operator.
The general form of such a special conversion member function is:
operator type() { ..... }

Using our my_string example, if we want a conversion from my_string to char*, we would define the following special conversion function inside the my_string class:

my_string::operator char*(){
 char* p = new char[len + 1];
 strcpy(s, p);
 return p;
}
Such a member function must be nonstatic. It cannot have parameters and does not have a declared return type. It must return an expression of the designated type. Notice that we did not simply return the value of the private member s. To do this would violate the integrity of my_string objects.