| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"John Goche" |
| Date: |
04 Sep 2006 05:03:15 AM |
| Object: |
"overloaded cast operator" and "operator const" |
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
.
|
|
| User: "Frederick Gotham" |
|
| Title: Re: "overloaded cast operator" and "operator const" |
04 Sep 2006 09:48:12 AM |
|
|
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 */
}
}
--
Frederick Gotham
.
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: "overloaded cast operator" and "operator const" |
04 Sep 2006 05:49:15 AM |
|
|
John Goche wrote:
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.
.
|
|
|
|

|
Related Articles |
|
|