Solved

unary condition operator

Posted on 2001-06-17
6
467 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
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 100 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now