Thread: C++ operator overloading

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Thumbs up C++ operator overloading

    can we ask ....... Operator overloading is also called the redesigning of language.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Not really, in my opinion. Mostly because operator overloading must have an argument of class type. You can't overload operators for built-in types.

    Also, you can't alter precedence and associativity rules and you lose control of short-circuit evaluation.

    Macros could perhaps be seen as a way to "redesign" a language. But operator overloading, not really.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You could overload minus to do addition, or plus to do subtraction for your class, but I wouldn't call that "redesigning of language" but rather "being stupid".

    A more logical unusual redefined operator is if you overload '<' and '>' to do something besides comparison. In most cases that would still be poor design in most cases.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Operator overloading is essentially a different way of writing a function.

    It's usually used when the normal operators won't work without defining some special behavior, such as adding two objects together.

    Say you want to "add" two shapes together. You can overload '+' and define what it means to combine the shapes, or you can write a function.

    IMHO - 99% of the time, it's better to write a function, because when you read (or edit) someone else's code, you'll see the operator and you might forget that it's overloaded.

  5. #5
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    I agree with DougDbug, I see the two similarly, and often when people write overloaded operators it causes more hassle then its worth.

    That is not to say they have no place, but if you do use them comment them well so you know whats happening.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Well, I don't think there is any need to do any excess operator overloading. Eg, for a string, the + operator is essential, but there is no need for a * one, why multiply two strings? However, if there is something you want to do, and you could use an operator for it, I would make a function for it rather than using an operator which may be somewhat ... confusing.

    Eg, if I had a string class, and I wanted to get rid of every instance of a sub string in it, I could use the - operator, or I suppose I could use the / one. However, they are both somewhat ambiguous, so I would think that to create a function to de-substring the string would be a better idea.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Overloading STL classes operators seems to me a bad idea, overall. And some operators in particular (< and == come to mind) would be particularly insiduous.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I should add, some opperators, such as '!', ',', and unairy '&' should never be overloaded.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Mario F.
    Overloading STL classes operators seems to me a bad idea, overall. And some operators in particular (< and == come to mind) would be particularly insiduous.
    That depends on the type of class your overloading for. Obviously the string class overloads those operators, but they do it in a logical way. Making a + operator subtract in your class or any other kind of illogical use would be insidious, but I think all operators available to be overloaded should be(assuming a need to do so) overloaded, as long as it's logical and responsible.
    Sent from my iPadŽ

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could overload + to do substraction. You could also write a function called "print" that reads a value from the console. It's exactly the same: misleading function names.
    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

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by CornedBee
    You could overload + to do substraction.
    I said insidious, not impossible.
    Sent from my iPadŽ

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I was mostly referring to King Mir's first post.
    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

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by King Mir
    I should add, some opperators, such as '!', ',', and unairy '&' should never be overloaded.
    never say never. Boost.shared_ptr overloads operator '!' to allow you to test the value of the contained pointer.

    which means you can do
    Code:
    boost::shared_ptr x(new Something());
    
    // later
    
    if (!x)
    {
        cout << "OMG, someone reset my pointer! you b@stards!!"
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> I should add, some opperators, such as '!', ',', and unairy '&' should never be overloaded.

    Yeah, I meant to ask you about that. Why should they never be overloaded?

Popular pages Recent additions subscribe to a feed