#include <iostream>
using namespace std;
class Foo{
string _s;
public:
Foo(string ss){
_s = ss;
}
Foo& operator=(bool b){
cout << "bool" << endl;
return *this;
}
Foo& operator=(const string& ss){
cout << "another one" << endl;
return *this;
}
};
int main(){
Foo f("bar");
f = "this";
return 0;
}
我已重載=
運算符。我預計f = "this";
聲明致電operator=(const string& ss)
過載。但事實並非如此。它稱爲operator=(bool b)
過載。爲什麼?奇怪的運算符重載行爲?
什麼?超負荷中只有一個UDT。 (除此之外,100%正確) – 2013-03-04 21:50:00
@MooingDuck是的,你是對的。當我沒有想到地鍵入答案時,這是有道理的! – juanchopanza 2013-03-04 21:50:56
+1:這可能會讓新手感到驚訝,因爲The Standard定義的類型將被歸類爲**用戶**定義的類型,但是您去了! – Johnsyweb 2013-03-05 00:42:43