C + + type conversion function: converts the type of the current class to other types
The conversion constructor can convert other types to the current class type (for example, convert double type to Complex type), but it cannot convert the current class type to other types (for example, convert Complex type to double type).
C + + provides a Type conversion function to solve this problem. The function of Type conversion function is to convert the current class type to other types. It can only appear in the form of member function, that is, it can only appear in the class.
The syntax format of the type conversion function is:
operator type(){ //TODO: return data; }
operator is the C + + keyword, type is the target type to be converted, and data is the data of type to be returned.
Because the target type to be converted is type, the return value data must also be type. Now that you know the data of type to be returned, there is no need to explicitly give the return value type like an ordinary function. The result of this is that the type conversion function does not appear to have a return value type, but actually implicitly indicates the return value type.
The type conversion function also has no parameters, because it is necessary to convert the object of the current class to other types, so the parameters are selfevident. In fact, the compiler assigns the address of the current object to the this pointer, so that the current object can be operated in the function body.
[example] add a type conversion function for the Complex class, so that the Complex type can be converted to double type.
#include <iostream> using namespace std; //Plural class class Complex{ public: Complex(): m_real(0.0), m_imag(0.0){ } Complex(double real, double imag): m_real(real), m_imag(imag){ } public: friend ostream & operator<<(ostream &out, Complex &c); friend Complex operator+(const Complex &c1, const Complex &c2); operator double() const { return m_real; } //Type conversion function private: double m_real; //real part double m_imag; //imaginary part }; //Overload > > operator ostream & operator<<(ostream &out, Complex &c){ out << c.m_real <<" + "<< c.m_imag <<"i";; return out; } //Overload + operator Complex operator+(const Complex &c1, const Complex &c2){ Complex c; c.m_real = c1.m_real + c2.m_real; c.m_imag = c1.m_imag + c2.m_imag; return c; } int main(){ Complex c1(24.6, 100); double f = c1; //Equivalent to double f = complex:: operator double (& C1); cout<<"f = "<<f<<endl; f = 12.5 + c1 + 6; //Equivalent to f = 12.5 + complex:: operator double (& C1) + 6; cout<<"f = "<<f<<endl; int n = Complex(43.2, 9.3); //Convert to double first and then int cout<<"n = "<<n<<endl; return 0; }
Operation results:
f = 24.6 f = 43.1 n = 43
In this case, the type conversion function is very simple, which is to return the member variable M_ The value of real, so it is recommended to write it in the form of inline.
The overloading of type conversion functions and operators is very similar. They both use the operator keyword. Therefore, type conversion functions are also called type conversion operators.
Description of type conversion function

Type can be builtin type, class type and type alias defined by typedef. Any type that can be returned as a function type (except void) can be supported. Generally speaking, it is not allowed to convert to array or function type. It is possible to convert to pointer type or reference type.

Type conversion functions generally do not change the converted object, so they are usually defined as const members.

Type conversion functions can be inherited and can be virtual functions.

Although a class can have multiple type conversion functions (similar to function overloading), if the target types to be converted by multiple type conversion functions can be converted to each other (similar types), ambiguity will sometimes occur. Take the Complex class as an example. Suppose it has two type conversion functions:
operator double() const { return m_real; } //Convert to double type operator int() const { return (int)m_real; } //Convert to int type Then the following wording will lead to ambiguity: Complex c1(24.6, 100); float f = 12.5 + c1;
The compiler can call operator double() to convert c1 to double type or operator int() to convert c1 to int type. Both types can be added with 12.5, and the conversion from Complex to double is the same as that from Complex to int, and no one has higher priority, So at this time, the compiler doesn't know which function to call. Just throw a ambiguous error and let the user solve it.