Operator overloading on "default" operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    Operator overloading on "default" operator

    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



  • Karl Heinz Buchegger

    #2
    Re: Operator overloading on "default&q uot; operator

    John Smith wrote:[color=blue]
    >
    > 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[/color]

    class A
    {

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

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Ivan Vecerina

      #3
      Re: Operator overloading on "default&q uot; operator

      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
      news:4163C07D.4 D618094@gascad. at...[color=blue]
      > John Smith wrote:[color=green]
      >>
      >> 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.[/color][/color]
      ....[color=blue]
      > class A
      > {[/color]
      public: // added for the sake of the discussion below[color=blue]
      > operator bool() { /* do whatever you need to do and return
      > };[/color]

      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:


      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


      Comment

      Working...