tags:

views:

179

answers:

5

I have the following code in a class:

operator string() {
        return format("CN(%d)", _fd);
}

And wanted to know what this operator does.

I am familiar with the usual string operators:

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;

... but not this one?

+1  A: 

If just returns a string represantation of your current object, e.g. for printing it on the console.

ZeissS
More specifically, it's a conversion operator for converting to type `string`. You can create conversion operators targeting other types too.
Chris Jester-Young
+6  A: 

It's overloading the cast operator. A class which has the function

operator string();

defined can be cast to a string.

Jonathan M Davis
it's got nothing to do with casting, it's type conversion, that that may happen due to a cast is another matter.
wich
A: 

That would appear to be a cast-to-string operator for a decimal number type.

Mike Burton
+21  A: 
operator Type() { ... }

is the (implicit) conversion operator. For example, if class Animal implements operator string(), then the code

Animal a;
...
do_something_with ( (string)a );

will become something like

do_something_with ( (Animal::operator string)(&a) );

See http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr385.htm for some more examples.

KennyTM
+1 for referring to it by its proper name, conversion operator.
Chris Jester-Young
A good example is the oft used `operator bool() { ... }` which allows an object of the type to be used in `if (object) { ... } else { ... }` which for example in container types could be implemented as meaning true to be non-empty.
wich
@wich: `operator bool` can give you unexpected behavior http://stackoverflow.com/questions/2145931/why-is-operator-bool-invoked-when-i-cast-to-long.
KennyTM
As a side note, such implicit conversions are generally considered a bad idea
jalf
David Rodríguez - dribeas
+1  A: 

It is an automatic type conversion operator. This class you are talking about can be implicitly converted to a string. This link might help you with a few more examples.

fogo