Solved

unary condition operator

Posted on 2001-06-17
6
474 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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