Operator Overload

This is a discussion on Operator Overload within the C++ Programming forums, part of the General Programming Boards category; Can someone please direct me to a nice, easy tutorial regarding the overloading of operators. My textbook isnt very clear ...

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Operator Overload

    Can someone please direct me to a nice, easy tutorial regarding the overloading of operators. My textbook isnt very clear and the sites i looked at seemed a bit too complex.

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried googling?

    Here's a selection that looks good from the first page of
    http://www.cs.caltech.edu/courses/cs...e/cpp-ops.html
    http://www.csse.monash.edu.au/~jonmc...html/text.html
    http://www.functionx.com/cpp/Lesson25.htm
    http://www.parashift.com/c++-faq-lit...erloading.html

    I didn't spend many seconds on each of those pages, so I can't say I "recommend" any of them

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Or even better, if you have a specific example you need some help with, there are many individuals here who can help you...

    But basically, the way to overload an operator follows ( loosely ) this general format:

    Code:
    return_type operatorXX( arguments ){ return statements }
    
    /*  Where XX is the operator in question */
    You have some flexibility in writing your oerators, just be careful to only write operators that make sense and that their return-type and use are intuitive to the end user. In other words overloaded operators shouldn't "confuse" a user.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Or in other words, don't overload a + operator that does subtraction (or the like)
    The overloaded operators should make sense!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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