This article quoted: http://www.jb51.net/article/41333.htm
Operator is used for type conversion functions:
Characteristics of a type conversion function:
1) Type conversion function is defined in the source class;
2) must be modified by operator, the function name is the target type name or the target class name;
3) The function has no parameters, there is no return value, but there is a return statement, the target type data is returned in the return statement, or the constructor of the target class is called.
object to basic data type conversions:
#include <iostream>#include<string>using namespacestd;classd{ Public: D (DoubleD):d _ (d) {}operator int()Const{std::cout<<"(int) d called!"<<Std::endl; returnstatic_cast<int>(D_);}Private: DoubleD_;};intAddintAintb) { returnA +b;}intMain () {D D1=1.1; D D2=2.2; Std::cout<<add (D1,D2) <<Std::endl; System ("Pause"); return 0;}
Use of operator in C + + (type cast member function)