Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++

Started by MubasharIqbal Jul 11, 2006 at 9:09 AM 4 replies 1.3k views
Original Post
MubasharIqbal
MubasharIqbal
Can we ask ...... Operator overloading is also called redesigning the language.
Mercury
Mercury
...Pardon?

Would you care to rephrase your question?
dave
dave
I suppose you could say that, but changing an operators operations to something vastly different to that of typical usage would render the operators largely confusing to use.
jpetrie
jpetrie
I disagree.

Operating overloading is a feature of the language; I wouldn't consider using it "redefining" the language. You can, of course, use operator overloading to redefine the semantics of the operator for your own custom types -- but that's not the same as changing the syntax or behavior of the language, its just allowing you to provide syntatic sugar to give the appearance of extending the language for a specific (custom) subdomain (you cannot overload operators for built-in types such as int + int).
Photonman
Photonman
Well, you could actually make a global function that overloads XOR (^) and change it to become a pow operator. Doing so, I suppose, would be redefining the language (sort of), however, you can't change the precendence of the operator, and XOR is much lower than exponent, so you would always have to use parens to get the precedence to work right. Why you would do this is beyond me.

Operator overloading, when used as it was intended, is a language feature that makes syntax easier when working with straightforward operations on custom types. For example, if you had a class for complex numbers, instead of having to call c.add(x); you can overload operator+ so you can do c+a; This leads to cleaner, easier to understand code, assuming the programmer makes sure that what the overloaded operators perform is straightforward. ex) + makes sense when used for string concatenation even though it is directly associated with numbers and not strings, but defining operator+ on some other kind of custom type may make sense to you and you alone.

In this capacity, you are simply altering your own types to act more like the concrete types of the language, not the reverse.
______Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Photonman
Well, you could actually make a global function that overloads XOR (^) and change it to become a pow operator. Doing so, I suppose, would be redefining the language (sort of), however, you can't change the precendence of the operator, and XOR is much lower than exponent, so you would always have to use parens to get the precedence to work right.

No, you can't actually. The only operators you are allowed to overload in a manner which redefines their default functionality for a given type or group of types are new, delete, new [], delete [], the comma operator, and the address of operator. You cannot change ^ to a power operator, though you may have it perform a power operation for user-defined types.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.