简体   繁体   中英

c++ operator overload

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

about this line : operator T() const {return my_t;} is this operator overload? but I am not so understand which operator is been overloaded? anybody can explain it for me? thanks!

Yes, that is operator overloading. The operator that has been overloaded is the casting operator to type T . That's the operator that is invoked when you ask the compiler to convert an Obj<T,X> into a T .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM