473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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)
Jul 22 '05 #1
2 6406
On Fri, 5 Mar 2004 15:50:45 +0100, "Jakob Bieling" <ne*****@gmy.net> 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
le**@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
Jul 22 '05 #2
Jakob Bieling wrote in news:c2*************@news.t-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/
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Clifton M. Bean | last post by:
First, I defined three classes (listed below): =========== // 1st class =========== class PointCl { public: PointCl & operator= (const PointCl & rgh ); //define as usual assingment operator
3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
3
by: Grant Austin | last post by:
Hi all, I'm trying to overload the '==' operator for a class, Page, that I wrote so that I can use the STL List 'find' algorithm. I'm having difficulties getting a definition that g++ likes and...
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
1
by: acheron05 | last post by:
Hi, I've been writing a program for another school assignment but I am having trouble working out how to overload the < and << operators. The program is designed to read data from a file, create...
0
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector...
9
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.