C++中没有返回类型的函数有3个,构造函数、析构函数、类型转换函数。ios
类型转换运算符,只要你把XXX对象隐式或者显式转换为T对象时,它都会被自动调用。函数
#include<iostream> using namespace std; //类型转换运算符重载,只要你把XXX对象隐式或者显式转换为T对象时,它自动被调用 template<class T> class Transfer { public: Transfer(int arg):i(arg){} operator T() const { return i; } private: int i; }; int main() { Transfer<double> t(3); //double d =static_cast<double>(t);//显示转换 double d = t;//隐式转换 cout<<d; getchar(); return 0; }