爲什麼此代碼不打印「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;
}
爲什麼此代碼不打印「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;
}
編譯器生成的拷貝賦值運算符是重載選擇:
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";
}
};
將打印「拷貝賦值」和基本上等於什麼編譯器會爲您(不印刷,課程)。
因爲它只是一個賦值運算符,所以不會阻止編譯器隱式生成_copy-assignment_運算符,這是實際在此調用的。 – ildjarn