473,322 Members | 1,504 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,322 software developers and data experts.

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

Jul 22 '05 #1
2 1185
John Smith wrote:

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


class A
{

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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
John Smith wrote:

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.
.... class A
{ public: // added for the sake of the discussion below operator bool() { /* do whatever you need to do and return
};


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:
http://www.artima.com/cppsource/safebool.html

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
Jul 22 '05 #3

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

Similar topics

8
by: whtevr | last post by:
Consider this HTML on an ASP page called default.asp in a virtual directory: <FORM name="Flogin" id="Flogin" action="" method=post> </FORM> Do I HAVE to put the literal "default.asp" into the...
5
by: cppaddict | last post by:
Hi, How, in general, does the default operator= work? That is, say I have a custom class MyClass and do: MyClass defaultMyClass; MyClass initializedMyClass("set","some","members");...
13
by: DaKoadMunky | last post by:
I recently came across some code in a template that default constructed an object of type T to pass to another function... SomeFunction(T()); The code that instantiates that template specifies...
9
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument....
12
by: Stefano | last post by:
Hi all, what is the correct use of the "default" attribute in XML Schema? For example: <xs:element name="myProperty" type="xs:string" default="myDefaultValue"/> What can I do with it? What...
5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
15
by: simonoficina | last post by:
Hello all! I am a vb.net beginner in Spain. When I use VB6 ,the button object has a property called "default" that can set this button like press "ENTER" key. But in the VB.net I can't find this...
3
by: kjworm | last post by:
Greetings, I have a report displayed on a screen that anyone in the office can look at. The data on this report will change from time to time. This report has different colors displayed...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.