Q: operator void* or operator bool?

Discussion in 'C++' started by Jakob Bieling, Mar 5, 2004.

  1. 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)
    Jakob Bieling, Mar 5, 2004
    #1
    1. Advertising

  2. Jakob Bieling

    Leor Zolman Guest

    On Fri, 5 Mar 2004 15:50:45 +0100, "Jakob Bieling" <> wrote:

    >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!


    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

    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
    Leor Zolman, Mar 5, 2004
    #2
    1. Advertising

  3. Jakob Bieling wrote in news:c2a442$5fn$05$-online.com:

    > 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.
    >


    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.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Mar 5, 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. Marcin Kalicinski

    void * instead of bool

    Marcin Kalicinski, Jun 21, 2004, in forum: C++
    Replies:
    18
    Views:
    915
    Prateek R Karandikar
    Jun 22, 2004
  2. Ollej Reemt
    Replies:
    7
    Views:
    518
    Jack Klein
    Apr 22, 2005
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    786
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    824
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    401
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page