//#include <string>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
template<class T, class X> class Obj{
T my_t;
X my_x;
public:
Obj(T t, X x):my_t(t),my_x(x){}
operator T() const {return my_t;}
};
int main()
{
int iT;
Obj<int, float> O(15,10.375);
iT = O;
cout << iT << endl;
return O;
}
关于这一行:operator T() const {return my_t;}
这个运算符是否过载?但我不太明白哪个运算符被重载了?
有人能为我解释一下吗?谢谢!
最佳答案
是的,那就是运算符重载。已重载的运算符是强制转换运算符,键入 T
。这是当您要求编译器转换 Obj<T,X>
时调用的运算符。进入T
.
关于C++ 运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5294189/