I have a question could a variable act as an operator such as "+" to add two numbers if so how do i write that? so what I would like to do would be answer = (a b c)
where a is some number b is an operator (such as +) and c is another number, Im not sure how to write that so i could get that line to work and help would be really appreciated
basically trying to a calculator without all the cases
You can do it, but it's quite a lot more complicated that using a switch with four to six cases. You have to #include <functional> then in the create an std::function<int(int,int)> use whatever type your operands are instead of int if they aren't int. You would then write answer = function(left,right) with function being whatever you called your function object. You would have to set function to a different function depending on what the input was, which could shorten your cases, but it's not worth the hassle.
tl;dr Yes you can have a variable that acts like a function, not an operator though the difference is basically semantic. But the resulting code really really will not be worth your time.
operators are methods that usually are used for non-primitive type . What I am saying is that
1 + 2 does not need you to overload the operator + . The moment the + or - operator becomes (in general speaking ... ) is when you sum matrices or vectors , which are not primitive type .
For a calculator exercice , you can try the parsing .
operators are methods that usually are used for non-primitive type .
No. Most operators are used for primitive types such as your example of 1+2.
What I am saying is that 1 + 2 does not need you to overload the operator +
This is entirely true but not entirely relevant. Just because you don't overload it does not mean it is not an operator, neither does it mean you cannot replicate its function.
The moment the + or - operator becomes (in general speaking ... ) is when you sum matrices or vectors , which are not primitive type .
This sentence doesn't make any sense and hence I can't really respond to it, but if you're saying that the moment + or - becomes an operator is when they're being used for non-primitive types is also purely false.
For a calculator exercise , you can try the parsing .
This has already been said. It also doesn't answer the question of how to implement it. (switch or map from char to function or whatever)
I agree on that , what I am trying to say it that we should see it like an arithmetic operation instead .
[quote]What I am saying is that 1 + 2 does not need you to overload the operator +
This is entirely true but not entirely relevant. Just because you don't overload it does not mean it is not an operator[/quote] Well I did not say it was not operator.
To answer to the parser thing there is an interressant link I found on Google :
If you need or expect to need to handle complex expressions? Then make a parser. Use Ericool link for (rather unintuitive but good) shunting yard algorithm or Precedence Climbing for more simple parser: http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm