我有一个类下面的代码:运算符string(){some code}做什么?
operator string() {
return format("CN(%d)", _fd);
}
,想知道该运营商做什么。
我熟悉常用的字符串操作符:
bool operator==(const string& c1, const string& c2);
bool operator!=(const string& c1, const string& c2);
bool operator<(const string& c1, const string& c2);
bool operator>(const string& c1, const string& c2);
bool operator<=(const string& c1, const string& c2);
bool operator>=(const string& c1, const string& c2);
string operator+(const string& s1, const string& s2);
string operator+(const Char* s, const string& s2);
string operator+(Char c, const string& s2);
string operator+(const string& s1, const Char* s);
string operator+(const string& s1, Char c);
string& operator+=(const string& append);
string& operator+=(const Char* append);
string& operator+=(const Char append);
ostream& operator<<(ostream& os, const string& s);
istream& operator>>(istream& is, string& s);
string& operator=(const string& s);
string& operator=(const Char* s);
string& operator=(Char ch);
Char& operator[](size_type index);
const Char& operator[](size_type index) const;
...但没有这一项?
+1用于指定它的专有名称,转换运算符。 – 2010-02-10 18:40:38
一个很好的例子是经常使用的'operator bool(){...}',它允许一个类型的对象用在'if(object){...} else {...}'中,例如在容器类型中可以实现为真正意义上的非空。 – wich 2010-02-10 18:49:03
@wich:'运营商布尔'可以给你意想不到的行为http://stackoverflow.com/questions/2145931/why-is-operator-bool-invoked-when-i-cast-to-long。 – kennytm 2010-02-10 18:56:35