Creating operators.....

This is a discussion on Creating operators..... within the C++ Programming forums, part of the General Programming Boards category; How can one create an operator to be used in their own program? For example, say I want the character ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Arrow Creating operators.....

    How can one create an operator to be used in their own program? For example, say I want the character '@' to be used for multiplying numbers together. How do the standard C++ operators work?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by Sentral
    How can one create an operator to be used in their own program? For example, say I want the character '@' to be used for multiplying numbers together.
    You can't. Plain and simple.
    Quote Originally Posted by Sentral
    How do the standard C++ operators work?
    They're little tokens that the compiler understands and can convert accordingly when it sees them.
    Sent from my iPad®

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    But you could maniuplate some operators to do something ... like you could overload the + operator in a class you made to subtract or something. Not the best of ideas though. It leads to confusion

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,516
    Well... you can create a @ operator and use it to perform multiplication, or addition, or whatever.

    You need to create a parser and a small lexer that will encapsulate the rules.

    it could be as simple as:

    Code:
    double calculate(int left_arg, int right_arg, char oper) {
        switch(oper) {
            case '@':
                 return left_arg * right_arg;
                 break;
            case /* ... */
                 /* ... */
        }
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by Mario F.
    Well... you can create a @ operator and use it to perform multiplication, or addition, or whatever.
    I believe he's referring to an operator to be used in the code's syntax, not inside of a program.

    However, you can in fact use a nonstandard operator to be used in your C++ code! All you have to do is write your own C++ compiler that doesn't completely conform to the standards. Enjoy!
    Sent from my iPad®

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,516
    He doesn't need a compiler for that either. Just a parser that translates his altered code to something C++ standard compiler can read.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Actually, the code that Mario F. posted might work! I don't want to change the C++ language, I just want some operators to make things easier for my specific program. I perhaps should've made that clearer? Anyway, I could just stick operators in a header file, and use the symbol rather than a call to a function, which is what I wanted. DANKE!

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Actually your first sentence was clear enough, but the
    How do the standard C++ operators work?
    Could be argued as being the exact opposite of clear. Some kind of... I don't know... non-anti-clearite... I don't know, I don't think there is a word for it. But that's what it is!
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. Creating new operators?
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2003, 12:20 PM
  5. Creating and overloading new binary operators.
    By HolySmiter in forum C++ Programming
    Replies: 7
    Last Post: 02-24-2002, 12:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21