#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