Solved

unary condition operator

Posted on 2001-06-17
6
472 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
Technology Partners: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

735 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