"overloaded cast operator" and "operator const"

J

John Goche

Hello,

Could anyone please provide with some information on the
C++ overloaded cast operator and in which circumstances
this might be useful? I have consulted several references
but found no information on the uses of this operator.

Thanks,

JG
 
R

Rolf Magnus

John said:
Hello,

Could anyone please provide with some information on the
C++ overloaded cast operator

It's a conversion operator. A "cast operator" doesn't exist.
and in which circumstances this might be useful?

It can be useful if you want to provide a conversion from your class into a
built-in type or another class that you can't change. In any other case,
you should prefer a conversion constructor.
 
F

Frederick Gotham

John Goche posted:
Hello,

Could anyone please provide with some information on the
C++ overloaded cast operator and in which circumstances
this might be useful? I have consulted several references
but found no information on the uses of this operator.

Thanks,

JG


Maybe something like:

class InputStream {
private:

bool success_last_write;

public:

InputStream &operator>>(int);

operator bool()
{
return success_last_write;
}
};

int main()
{
InputStream is;

int i;

if(is >> i)
{
/* Do something */
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top