LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2003, 04:18 PM   #1
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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,
 
Old 11-02-2003, 05:12 PM   #2
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Would a cast operator to bool work?

Code:
operator bool() const {return isThisObjectTrueOrFalse();}
Cheers..
 
Old 11-02-2003, 05:16 PM   #3
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Original Poster
Rep: Reputation: 30
WOOHOO ..it works, thanks man.

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

thanks again,
 
Old 11-02-2003, 05:37 PM   #4
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Original Poster
Rep: Reputation: 30
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,
 
Old 11-02-2003, 07:48 PM   #5
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
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.
 
Old 11-02-2003, 08:21 PM   #6
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Original Poster
Rep: Reputation: 30
cool,

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

thanks again,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Netatalk opposite phatbastard Linux - Software 2 11-18-2005 04:12 AM
load(lin) -- the opposite JrLz Linux - General 2 05-09-2005 02:16 AM
the opposite command of su? can i access user logins when i am root? how? kublador Linux - Newbie 3 09-11-2003 03:43 AM
Opposite to modprobe? Thymox Linux - Software 4 12-11-2002 10:40 AM
finding the opposite... PokerFace Linux - General 1 10-16-2002 08:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration