为什么这段代码不打印“operator =”?
#include <iostream>
using namespace std;
class A{
public:
template<typename T> void operator=(const T& other){
cout<<"operator="<<endl;
}
};
int main(){
A a;
A b;
a=b;
}
答案 0 :(得分:7)
编译器生成的复制赋值运算符由重载决策选择:
class A{
public:
A& operator=(A const& other){
std::cout << "copy assignment\n";
return *this;
}
template<class T>
void operator=(T const& other){
std::cout << "templated assignment\n";
}
};
将打印“复制分配”并且基本上等于编译器将为您生成的内容(当然没有打印)。