LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   what's the opposite of operator!() (https://www.linuxquestions.org/questions/programming-9/what%27s-the-opposite-of-operator-111591/)

jhorvath 11-02-2003 04:18 PM

what's the opposite of operator!()
 
i can't seem to find the opposite of 'operator!()'

operator!() lets me do this ::
Code:

    if(!myClass) { /*...*/ }
but i would also like to do this ::
Code:

    if(myClass) { /*...*/ }
does anyone know how i could do this ...i'm assuming it's some operator#() function ..but i can't find which one :)

thanks,

mr_segfault 11-02-2003 05:12 PM

Would a cast operator to bool work?

Code:

operator bool() const {return isThisObjectTrueOrFalse();}
Cheers..

jhorvath 11-02-2003 05:16 PM

WOOHOO ..it works, thanks man.

i'm not exactly sure why ...i'll have to look that up

thanks again,

jhorvath 11-02-2003 05:37 PM

if my understanding is correct...

the operator bool() is a conversion operator, it converts it's return value to type bool? the if-statement takes a relational argument, and when i pass my object to it...the compiler automatically sees that it can 'convert' it into bool via that operator bool(), so it is now accepted as if the object itself were == to a relational value (true,false), based on what it had returned (actually i have it return 'true' or 'false' depending on a 'FILE * stream' being NULL or not) ..now since i'm am actually returning a 'legal' bool value from the start , there should be no foul play that would be involved in an implicit cast (or no?)

....am i close here :)

thanks,

mr_segfault 11-02-2003 07:48 PM

jhorvath,

Thats right, although I'm not familiar with the term relational being used to describe boolean expresstions.. I'd describe the 'if' statement as taking a boolean expression (ie one that is either true or false).

operator bool() is a conversion operator, I'll pinch/paraphrase some text from "The ANSI/ISO C++ Profession Programmer's Handbook":

Quote:

A conversion operator, can be thought of as a user defined typecasting operator; it converts its object to a different type in contexts that require that specific type.

Conversion operators differ from ordinary overloaded operators in two way. First, a conversion operator does not have a return value (not even void). The return value is deduced from the operators name. Secondly, a conversion operator takes no arguments.

Cheers.

jhorvath 11-02-2003 08:21 PM

cool,

thanks for going out of your way to find that ...i do appreciate it :)

thanks again,


All times are GMT -5. The time now is 03:16 PM.