what's the opposite of operator!()
i can't seem to find the opposite of 'operator!()'
operator!() lets me do this :: Code:
if(!myClass) { /*...*/ } Code:
if(myClass) { /*...*/ } thanks, |
Would a cast operator to bool work?
Code:
operator bool() const {return isThisObjectTrueOrFalse();} |
WOOHOO ..it works, thanks man.
i'm not exactly sure why ...i'll have to look that up thanks again, |
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, |
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:
Cheers. |
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. |