A type conversion operator is a special class member function that defines a transformation that converts a class type value to another type value. The conversion operator is declared in the class definition body, followed by the target type of the transformation after the reserved character operator. A conversion function is also called a type cast member function, which is a non-static member function in a class. It is defined in the following format:
Copy Code code as follows:
Class < type descriptor 1>
{
Public
operator < type specifier 2> ();
...
}
This transformation function defines the mapping relationship between the < type specifier 1> to the < type descriptor 2>. Visible, conversion functions are used to convert one type of data to another.
1.operator for type conversion functions:
Characteristics of type Conversion functions:
1) The type conversion function is defined in the source class;
2) must be decorated by operator, the function name is the target type name or target class name;
3 The function has no parameters, no return value, but there is a returns statement that returns the target type data or calls the constructor of the target class in the return statement.
There are two main categories of type conversion functions:
1 the object is converted to the basic data type:
Copy Code code as follows:
#include <iostream>
#include <string>
using namespace Std;
Class d{
Public
D (Double D):d _ (d) {}
operator Int () const{
std::cout<< "(int) d called!" <<std::endl;
Return static_cast<int> (D_);
}
Private
Double D_;
};
int add (int a,int b) {
return a+b;
}
int main () {
D d1=1.1;
D d2=2.2;
Std::cout<<add (D1,D2) <<std::endl;
System ("pause");
return 0;
}
Results:
It is visible that the operator int () const function is implicitly invoked when the Add (D1,D2) function is called.
2 Conversion of objects to objects of different classes:
Copy Code code as follows:
#include <iostream>
Class X;
Class A
{
Public
A (int num=0):d at (num) {}
A (const x& RHS):d at (RHS) {}
operator Int () {return dat;}
Private
int dat;
};
Class X
{
Public
X (int num=0):d at (num) {}
operator Int () {return dat;}
Operator A () {
A Temp=dat;
return temp;
}
Private
int dat;
};
int main ()
{
X stuff=37;
A more=0;
int hold;
Hold=stuff;
std::cout<More=stuff;
std::cout<<more<<std::endl;
return 0;
}
The X class in the above program uses the "operator A ()" type conversion to convert the X type object to A type A.
2. Operator for operator overloading:
The concept of operator overloading:
Associate an existing operator with a member function and use the operator with its member objects (operands).
Precautions:
1 overload cannot change the basic function of the operator, and the order of precedence of the operator.
2 overload should not change the original meaning of the operator.
3 can only overload an existing operator, but not the new symbol.
4 operator overloading is only available for classes.
5 The following operators cannot be overloaded:
. Origin operator (member accessor)
* Pointer to member
:: scope-resolution characters
? : Question mark conditional operator
Number of bytes of sizeof operand
General format for operator functions:
Return_type operator op (argument list);
Return_type: Return type (what to get)
OP: operator to overload
Argument list: Parameter lists (what operands are)