C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q: operator void* or operator bool?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jakob Bieling
Guest





PostPosted: Fri Mar 05, 2004 2:50 pm    Post subject: Q: operator void* or operator bool? Reply with quote



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)


Back to top
Leor Zolman
Guest





PostPosted: Fri Mar 05, 2004 3:07 pm    Post subject: Re: Q: operator void* or operator bool? Reply with quote



On Fri, 5 Mar 2004 15:50:45 +0100, "Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote:

Quote:
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
[email]leor (AT) bdsoft (DOT) com[/email]
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

Back to top
Rob Williscroft
Guest





PostPosted: Fri Mar 05, 2004 4:27 pm    Post subject: Re: Q: operator void* or operator bool? Reply with quote



Jakob Bieling wrote in news:c2a442$5fn$05$1 (AT) news (DOT) t-online.com:

Quote:
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 ? "? Truen" : "? Falsen" );

ex.condition = true;

cerr << ex << 'n';
cerr << ( ex ? "? Truen" : "? Falsen" );

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

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

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.