Q: operator void* or operator bool?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jakob Bieling

    Q: operator void* or operator bool?

    Hi,

    I remember reading a document that advised to prefer 'operator void*'
    over 'operator bool' or other way round, when I want to provide the ability
    to use code like this:

    test_class t;
    while (t)
    {
    // do stuff
    };

    But I cannot remember why and which method was prefered.

    Thanks for the help!
    --
    jb

    (replace y with x if you want to reply by e-mail)


  • Leor Zolman

    #2
    Re: Q: operator void* or operator bool?

    On Fri, 5 Mar 2004 15:50:45 +0100, "Jakob Bieling" <netsurf@gmy.ne t> wrote:
    [color=blue]
    >Hi,
    >
    > I remember reading a document that advised to prefer 'operator void*'
    >over 'operator bool' or other way round, when I want to provide the ability
    >to use code like this:
    >
    >test_class t;
    >while (t)
    >{
    > // do stuff
    >};
    >
    > But I cannot remember why and which method was prefered.
    >
    >Thanks for the help![/color]

    To quote from Eckel/Allison's "Thinking in C++ Volume Two: Practical
    Programming" (the footnote on page 167):

    "It is customary to use operator void *() in preference to operator bool()
    because the implicit conversions from bool to int may cause surprises,
    should you incorrectly place a stream in a context where an integer
    conversion can be applied. The operator void*() function will only be
    called implicitly in the body of a Boolean expression."


    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html

    Comment

    • Rob Williscroft

      #3
      Re: Q: operator void* or operator bool?

      Jakob Bieling wrote in news:c2a442$5fn $05$1@news.t-online.com:
      [color=blue]
      > Hi,
      >
      > I remember reading a document that advised to prefer 'operator
      > void*'
      > over 'operator bool' or other way round, when I want to provide the
      > ability to use code like this:
      >
      > test_class t;
      > while (t)
      > {
      > // do stuff
      > };
      >
      > But I cannot remember why and which method was prefered.
      >[/color]

      Well Leor's answered that, but if you want some extra safety:

      #include <iostream>
      #include <ios>

      struct bool_as_member_ helper
      {
      int i;
      };
      typedef int (bool_as_member _helper::*bool_ as_member_ptr);


      struct example
      {
      operator bool_as_member_ ptr ()
      {
      return condition ? &bool_as_member _helper::i : 0;
      }
      bool condition;
      };

      struct bad
      {
      operator void * () { return 0; }
      };

      int main()
      {
      using namespace std;

      cerr << boolalpha;

      example ex = { false };

      cerr << ex << '\n';
      cerr << ( ex ? "? True\n" : "? False\n" );

      ex.condition = true;

      cerr << ex << '\n';
      cerr << ( ex ? "? True\n" : "? False\n" );

      cerr << "void * (boolalpha):\n" << bad() << '\n';
      }

      This method also avoid's the unwanted conversion to void *.

      Rob.
      --

      Comment

      Working...