Operator overloading on "default" operator

Discussion in 'C++' started by John Smith, Oct 6, 2004.

  1. John Smith

    John Smith Guest

    Hello,

    I made a class which works like bool primitive but has some special
    properties.
    Now I want to be able to overload an operator to be able to do the
    following:

    while (myObj)
    {
    ....
    }

    It must return true/false naturally, but I'm unsure if it's actually
    possible and if so which operator I should overload.

    Thanks in advance.
    -- John
    John Smith, Oct 6, 2004
    #1
    1. Advertising

  2. John Smith wrote:
    >
    > Hello,
    >
    > I made a class which works like bool primitive but has some special
    > properties.
    > Now I want to be able to overload an operator to be able to do the
    > following:
    >
    > while (myObj)
    > {
    > ...
    > }
    >
    > It must return true/false naturally, but I'm unsure if it's actually
    > possible and if so which operator I should overload.
    >
    > Thanks in advance.
    > -- John


    class A
    {

    operator bool() { /* do whatever you need to do and return
    a boolean value */ }
    };

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 6, 2004
    #2
    1. Advertising

  3. "Karl Heinz Buchegger" <> wrote in message
    news:...
    > John Smith wrote:
    >>
    >> Hello,
    >>
    >> I made a class which works like bool primitive but has some special
    >> properties.
    >> Now I want to be able to overload an operator to be able to do the
    >> following:
    >>
    >> while (myObj)
    >> {
    >> ...
    >> }
    >>
    >> It must return true/false naturally, but I'm unsure if it's actually
    >> possible and if so which operator I should overload.

    ....
    > class A
    > {

    public: // added for the sake of the discussion below
    > operator bool() { /* do whatever you need to do and return
    > };


    This is the "obvious" solution, but has well-known caveats,
    because bool-s are implicitly convertible to int.
    For example, the following expressions become valid:
    A a;
    int b = 5+a;
    a << 5;

    This is why usually library writers now prefer to provide
    a conversion operator to void* or to a (member) function pointer.
    For a discussion, see for example:
    http://www.artima.com/cppsource/safebool.html

    My personal preference, for internal code, is to implement
    operator ! only, because I like to use !! to explicitly
    convert values to bool -- e.g. I prefer if( !! myPtr )
    to if( myPtr != NULL ) or if( myPtr ) .


    Cheers,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Oct 6, 2004
    #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. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,407
    Fredrik Lundh
    Sep 30, 2005
  2. Replies:
    11
    Views:
    728
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    728
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    558
  5. Replies:
    2
    Views:
    311
Loading...

Share This Page