Thread: Why doesn't C++ allow one to create a new operator?

  1. #1
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Question Why doesn't C++ allow one to create a new operator?

    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.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    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

    Code:
    Class operator+(const Class &) const;
    You might want to return the Class by reference, unless it's a local reference
    Last edited by Daggie; 01-01-2003 at 05:15 AM.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    I think it was probably decided that maintaining code would be a nightmare if there were new operators contained within.
    Joe

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I think that defining new operators makes the code not readable...
    none...

  5. #5
    what, have you run out of the ones that come with C++? You don't need to overload operators for everything, just common tasks.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Cool

    Dumb me! Yes, you cannot create new operators - but you can overload the new and new[] operators.
    Mr. C: Author and Instructor

  9. #9
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Sometimes I want to create POWER operator (**), that we can't. But it's okay, math.h can provide this. .
    But it would be very great, if creating of new oper was allowed.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  3. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM
  4. problem with overloader bracker operator
    By cdonlan in forum C++ Programming
    Replies: 8
    Last Post: 04-11-2005, 02:05 PM
  5. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM