我有以下代码:
#include <iostream>
using namespace std;
template<typename T> class myclass {
public:
T data;
myclass(T const & _data = T()) : data(_data) {}
template<typename U> myclass<T> & operator=(myclass<U> const & rhs) {
cout << data << " = " << rhs.data << endl;
return *this;
}
};
int main() {
myclass<double> first(1);
myclass<float> second(2);
myclass<double> third(3);
first=second;
first=third;
}
现在,虽然它编译得很好,但输出只是:
1 + 2
为什么 first=third 不打电话给
myclass<double> & operator=(myclass<double> const & rhs)
?