C++ 运算符重载

标签 c++

//#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/

相关文章:

c++ - 为什么我可以通过指针访问私有(private)数据成员,我应该这样做吗?

c++ - 用于泄漏检测的 Appverifier 和 Visual Studio

c++ - 用于极端图像缩小的快速、高质量像素插值

c++ - long long 转换为字节数组

c++ - 最近的邻居用nanoflann搜索

c++ - std::filebuf VS2015 错误

c++ - 英特尔 13.1.2 中带有已删除复制构造函数的不良 C++ 行为的解决方法

c++ - 如何在 Lua 中处理 C++ 类构造函数失败

c++ - 编写代码使年利率翻倍

c++ - Cuda中的数组乘法产生错误