Jump to content

overloading operator question

- - - - -

  • Please log in to reply
2 replies to this topic

#1
shahar emule

shahar emule

    Newbie

  • Members
  • PipPip
  • 20 posts
hello all, whats up? i have a question about casting and overloading operators.
can i invent in my code new operator like =-%^ for example, or use only the existed (to rewrite them).
thank you a lot
shahar:rolleyes:

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You cannot invent your own operators, as operators are integral to the language's grammar. You would have to write your own adaptation of the language and build a compiler in order to invent new operators.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
You can only overload the existing operators (or it wouldn't be overloading them, now would it :)). But, just because you can doesn't mean you should. When overloading operators make sure that the overload makes sense without complex explanations. For example the String class overloads the + operator to perform concatenation. This makes 'sense'. But it doesn't overload the - operator as what would it mean? Remove those characters? Remove that word? Remove it if it's found on the end? etc.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users