Solved

unary condition operator

Posted on 2001-06-17
6
468 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

867 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

17 Experts available now in Live!

Get 1:1 Help Now