?
Solved

unary condition operator

Posted on 2001-06-17
6
Medium Priority
?
478 Views
Last Modified: 2011-04-14
Hi who knows what member function is invoked for an if test?

For example if I have

class CTest
{
  //   ... various stuff
  }

...

CTest t;
if(t){
  // do something
  }


What member function do I need to define to control how the if test behaves?

Thanks
0
Comment
Question by:GGRUNDY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 400 total points
ID: 6199423
Use bool operator.
Example:
class CTest
{
public:
     CTest(){};
     operator bool()
     {
          return false;
     }
     int data;
     //   ... various stuff
};

CTest xx;
if (xx)
{
     printf("true");
}
else
{
     printf("false");
}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6199455
There are /several/ operators that can be invoked, for instance, operator bool(), operator int(), any operator returning a pointer etc...

Using operator bool() is a dangerous design, operator bool() woul also give the compiler a way to coerce an object to int.

The CTest object xx from the previous example would be happy to help you with a statement like
  int i = xx;
 
Advice is to /not/ use operator bool() for if() tests, rather use operator !(). That has to be invoked explicitly.

class CTest
{
   public:
      bool operator ! ()
      {
         if (every_thing_ok) return false;
         else return true
      }
};

CTest xx;
if (!xx)
{
    printf("Bad Situation");
}
else
{
    printf("Everything is fine");
}
0
 
LVL 3

Author Comment

by:GGRUNDY
ID: 6199512
Thanks for your help gentlemen. And thanks for the words of caution Mr Roo. (I was already using operator!)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:Exceter
ID: 8576082
>> Using operator bool() is a dangerous design, operator bool() woul also give the compiler a way to coerce an object to int.

Perhaps I am confused, I don't understand why the bool operator would allow an object to be converted to an int. Since this operator returns a bool I would think that it is simply returning a 1 or a 0 and not a pointer/reference to the object itself. How is it different from saying,

int x = 5 < 8;

Personally, I like this sort of functionality as it is handy from time to time. If it is really dangerous, why was it used in the implementation of iostream? For example,

ifstream in("blah.txt");
string s;
while( in )
{
    in >> s;
    cout << s << endl;
}

in.close();

However, if I am mistaken, please point out my misgivings as I am considering using this operator in a class I'm developing.

Exceter
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 8576866
It also allows:

CTest t;
int x;
x = t;

You might not write that on purpose, but accidents happen - and it's a Good Thing if your compiler helps you to prevent accidents from happening. We've 'suffered' enough from precisely this mistake to rewrite a library of utility classes. The rewrite cost us less time then tracking down this simple bug.

> If it is really dangerous, why was it used in the implementation of iostream

Personally I consider that a mistake. It allows, simple mistakes to go undetected. And there is no reason to use operator bool(), since operator !() can be used for the same purpose and without the ambiguities.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8579021
>> It allows, simple mistakes to go undetected.

Perhaps, but I don't think I'll have much of a problem avoiding that.

>> And there is no reason to use operator bool(), since operator !() can be used for the same purpose and without the ambiguities.

True, but that hijacks the logical NOT operator, which I don't really want to do, and, in my opinion, this can make for some ambiguous code as well. At any rate, thanks for clearing that up.

Cheers!
Exceter
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question