Why wasn't C++ made to allow one to create a new operator? That seems to me like it would be a very useful feature instead of forcing one to reuse existing operators. Could someone please tell me why C++ was mad that way?
Why wasn't C++ made to allow one to create a new operator? That seems to me like it would be a very useful feature instead of forcing one to reuse existing operators. Could someone please tell me why C++ was mad that way?
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
first of all, I'm new here, so don't shoot me
Are you asking if it's possible to overload operators ? because that's possible ...
To add objects of Classes to eachother for example ... (operator+)
edit : operator+ should be prototyped something like
You might want to return the Class by reference, unless it's a local referenceCode:Class operator+(const Class &) const;
Last edited by Daggie; 01-01-2003 at 05:15 AM.
I think it was probably decided that maintaining code would be a nightmare if there were new operators contained within.
Joe
I think that defining new operators makes the code not readable...
none...
what, have you run out of the ones that come with C++? You don't need to overload operators for everything, just common tasks.
A lot of problems would arise if you were able to make your own operator. Just to name a couple:
1) Ambiguity. What if someone created a binary operator |& and a binary operator |&| and a unary operator |. What would the expression 1 |&| 2 mean? Would it be a single operation using |&| or would it be 2 operations using |& and |? It seems like an odd situation, but stuff like that can happen pretty quickly and it makes things very confusing to people using the language, especially when each of the operators may have been created by different people that end up being used in the same program. You'd have to use parenthesis in most cases to avoid ambiguity which my be created after another operator is created at a later time. With the need for all the parenthesis, you might as well just make it a function.
2) Order of operations: The biggee. Multiplication comes before addition. Dereferencing happens before arithmetic operators. The scope operator comes before the address of operator. Let's say you wanted to make an operator -- what would its presedence be? Lowest? Highest? What about it's presedence in comparison to other newly created operators? Would you be able to "insert" a degree of presedence between addition and multiplication? How would you denote the presedence when you defined the operation. It would make things too complex to implement for someone making an operator and very difficult to follow for someone who's using it and you'd again have to use parenthesis to make everything clear.
On top of that there are a few smaller problems such as the naming restrictions. Certainly it wouldnt follow the rules of standard identifiers. There would have to be a whole new set of rules for that.
It would just make things much too confusing and cause way too many problems.
Last edited by Polymorphic OOP; 01-01-2003 at 10:11 AM.
Hehe, it is possible in Omicron.
The ambiguity can be resolved, although the issue with predecence would be harder to solve in C++, because C++ doesn't define operator predecence, it is just a consequence.
(EDIT)
This is from the C++ standard doccument:
The precedence of operators is not directly specified, but it can be derived from the syntax.
Last edited by Sang-drax; 01-01-2003 at 10:18 AM.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Dumb me! Yes, you cannot create new operators - but you can overload the new and new[] operators.
Mr. C: Author and Instructor