Ho il seguente codice in una classe:cosa fa l'operatore string() {qualche codice}?
operator string() {
return format("CN(%d)", _fd);
}
e voleva sapere che cosa questo operatore fa.
mi è familiare con i soliti operatori di stringa:
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;
... ma non questo?
+1 per riferirsi ad esso con il proprio nome, operatore di conversione. –
Un buon esempio è l'usato 'operator bool() {...}' che permette ad un oggetto del tipo di essere usato in 'if (oggetto) {...} else {...}' che per esempio nei tipi di contenitori potrebbe essere implementato nel senso che significa essere non vuoto. – wich
@wich: 'operator bool' può darti un comportamento inaspettato http://stackoverflow.com/questions/2145931/why-is-operator-bool-invoked-when-i-cast-to-long. – kennytm