"overloaded cast operator" and "operator const"

Discussion in 'C++' started by John Goche, Sep 4, 2006.

  1. John Goche

    John Goche Guest

    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
     
    John Goche, Sep 4, 2006
    #1
    1. Advertising

  2. John Goche

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Sep 4, 2006
    #2
    1. Advertising

  3. 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
     
    Frederick Gotham, Sep 4, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vineoff

    operator const char*()

    vineoff, Nov 12, 2005, in forum: C++
    Replies:
    11
    Views:
    2,898
    vineoff
    Nov 12, 2005
  2. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,044
    Earl Purple
    Jan 18, 2006
  3. Replies:
    1
    Views:
    422
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    408
    Victor Bazarov
    May 23, 2007
  5. thomas
    Replies:
    1
    Views:
    380
    Alf P. Steinbach
    Feb 14, 2008
Loading...

Share This Page