473,748 Members | 2,551 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 1196
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
5742
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
2109
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
2279
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
3719
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
4951
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
3913
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
3512
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
12545
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
2558
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
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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
9386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8255
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...
1
6799
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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
4608
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...
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.