operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; In a class I am creating I forsee the possible use of doing something like if(!class) or if(class). I have ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    Question operator overloading

    In a class I am creating I forsee the possible use of doing something like if(!class) or if(class). I have been able to overload the operator ! for doing if(!class), but what do I overload if I just want to have it as if(class)? (if this is even possible)?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Not possible IIRC.
    Woop?

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    A single const IsValid() method would be preferable. I'm pretty sure you can overload a type casting operator. Normally I'd say overload operator (bool) but as this is C/C++ I guess it will be something cryptic and totally out of the blue/in your face fact like overloading operator (void*).

    Overloading the operators might be fun, but writing a function will be easier to implement and easier to understand later. If you are really interested in overloading the typecast operators, you might want to look at an implementation of the file streaming classes in std::, they implement something like that.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Yes I've read a lot about how operator overloading gets misused a bunch especially by novice programmer like myself, but I think this is generally a good idea that I have. Right now I am building an image class and the only data member that I have is a surface pointer, with a bunch of methods to manipulate this (like draw to screen or load image etc). Prior to building this class I would always interact with the surface directly and to test whether or not my image had successfully loaded to the surface I would try something like
    Code:
    if(!surface){ //image failed to load }
    Now that I'm building this class I want to make it feel as though the class is still a surface or image that I am manipulating and would like to test whether or not the image has loaded by doing if(!class). I don't know.. this seemed intuitive to me so I thought it would be appropriate.

    Just wanted to add that after I have implemented this operator overload, while it is intuitive for me to still do this, I have run into the fact that if my class is a pointer that was assigned memory via new, I must call it as if(!*class) otherwise if(!class) is telling me whether or not the class pointer is null
    Last edited by ConsulVortex; 12-23-2005 at 04:15 AM.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Code:
    #include <iostream>
    
    using namespace std;
    
    #define B2T( var ) (var ? "true" : "false")
    
    class CExample
    {
    private:
    	bool m_bValid;
    
    public:
    	CExample( bool bValid )
    	{
    		cout << "constructor: " << B2T( bValid )  << endl;
    		m_bValid = bValid;
    	}
    
    	operator bool ()
    	{
    		cout << "typecast to bool" << endl;
    
    		return m_bValid;
    	}
    };
    
    int main()
    {
    	CExample ex( true ); // switch this line to "false" for testing reverse
    	
    	cout << "explicitly typecasted ex is " << B2T( (bool)ex ) << endl;
    
    	cout << "implicitly typecasted ex is " << B2T( ex ) << endl;
    
    	if( ex )
    	{
    		cout << "ex evaluated to true in if() construct" << endl;
    	}
    	else
    	{
    		cout << "ex evaluated to false in if() construct" << endl;
    	}
    
    	return 0;
    }
    If you implement a typecast operator for bool, it will work the way you want it. Note that you might get in trouble when you implement an operator that also casts to an integer value ( void*, int, long, char ) because the compiler might chose another implicit conversion. Naturally this will occur at the most unpleasant time possible, deadline near or over, valentines day without a gift yet and running out of coffee. It is possible as outlined above, but it will bite you in your behind when you expext it least. Go the boring but safe way and implement an IsValid() method.

    btw: the B2T macro is just for output candy, you can remove it if 0 for false and 1 for zero are okay for you, the example will work the same.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Implementing an operator int() is also an option, rather than operator bool().

    Incidentally, a more common name for such operators is a conversion operator, not a typecast. A cast is just a brute-force conversion.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I guess it will be something cryptic and totally out of the blue/in your face fact like overloading operator (void*).
    ifstream and co. do indeed use void*. Anyone know why?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes. It's to make things like this work:
    Code:
    if(surface)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by dwks
    Yes. It's to make things like this work:
    Code:
    if(surface)
    I'm sure everyone on the board will benefit from such an insightful explanation.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Sorry. Well, there isn't really an operator involved. It calls operator void*.

    I don't know much about it myself, in fact I just read it in a book about three days ago. Can you post an "insightfull explanation"?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Can you post an "insightfull explanation"?
    How about an explanation of the thread?

    nvoight: You can define an operator bool() so that you can write things like:

    if(classObject)....

    but it can lead to problems.

    anonytmouse: fstreams define operator void*() to enable you to use fstream objects in a boolean context. Does anyone know why they do that instead of defining an operator bool()?

    dwks: Yes. It's to make things like this work:

    if(classObject)....
    As for why operator void*() is preferred, see here

    http://groups.google.com/group/comp....&start=1&num=3
    Last edited by 7stud; 12-23-2005 at 11:19 PM.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by 7stud
    As for why operator void*() is preferred, see here

    http://groups.google.com/group/comp....&start=1&num=3
    Neither the bool() nor the int() are good choices because bools and
    ints they have lots and lots of operators related to them. This means
    that in addition to enabling the desired "if (a)" functionality, you
    would also enable stuff like "a+b", "a < 1",... Essentially, everything
    that you can do with an integral arithmetic type. The set of things
    you can accidentally do with a void*() is much smaller, so it is
    much safer.
    Thanks mate, that makes perfect sense.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by nvoigt
    btw: the B2T macro is just for output candy, you can remove it if 0 for false and 1 for zero are okay for you, the example will work the same.
    Rather off-topic, but std::boolalpha is a better choice.
    Code:
    cout << boolalpha << true << ' ' << false << noboolalpha << ' ' << true << ' ' << false << endl;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21