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?
Printable View
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?
You can't. Plain and simple.Quote:
Originally Posted by Sentral
They're little tokens that the compiler understands and can convert accordingly when it sees them.Quote:
Originally Posted by Sentral
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
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 /* ... */
/* ... */
}
}
I believe he's referring to an operator to be used in the code's syntax, not inside of a program.Quote:
Originally Posted by Mario F.
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!
He doesn't need a compiler for that either. Just a parser that translates his altered code to something C++ standard compiler can read.
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! :)
Actually your first sentence was clear enough, but theCould 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!Quote:
How do the standard C++ operators work?