473,699 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1194
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******@gasca d.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
5735
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 action attrib to get this form to post back to the "default.asp" page in that directory?
5
2104
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"); defaultMyClass = initializedMyClass;
13
2275
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 T as an int. Proper program behavior relies on that "default constructed" int being zero. That lead me to the following example...
9
3713
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. The elements of the array are constructed using the default > constructor for the instantiating type T. Does that mean that, on built-in types (e.g. int), "default initialization" (as described in 8.5) is performed?
12
4945
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 is the meaning of < ....default="myDefaultValue" /> ?
5
3908
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 with the same solution. All had "Default" set in the "Struct Member Alignment" entry. After some assembler debugging we found out that a struct member that is a member function pointer in
8
3503
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 cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
15
12535
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 property. Where is it? Or mabye change the other thing,No?Thanks ! Simon
3
2554
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 depending on the data. Since I'm using Access '97, I can't do this on a form. What I am doing is "refreshing" this report every 60 seconds. Since Access '97 can't use the timer interval for reports, I have a form that is closing and then reopening my...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8895
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7781
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.