Sign in to follow this  
tom_mai78101

[Concept] Possible to invoke an operator-overloaded function from strings?

Recommended Posts

The concept is that, for a given string, and a given iterator at the beginning of the given string, once the iterator reaches a specific character (like, '+', or '-') in the string, it will be able to invoke an operator-overloaded function (like operator+, operator-), do the actions provided in those functions, and let the iterator to continue scanning until it reaches the end of the string.

Is it possible? To call an operator-overloaded function just from scanning via an iterator?

I have this idea in my head, but logic deduction gives me errors. It doesn't seem possible, unless clarified.

Share this post


Link to post
Share on other sites
For the most part, the only way to get strings to call functions is to either do if(the_string=="+"){ do_plus_stuff(...);} and so on, or build a map of strings to function pointers. Operator overloading doesn't really buy you anything, unless you're already using it.

Share this post


Link to post
Share on other sites
You seem to be trying to write a parser. There are several ways of doing this. Of the top of my head:
* Write your own functions to parse expressions, terms, factors... (a.k.a. recursive descent parser).
* Use a parser generator, like yacc or bison.
* Write an LL parser by hand (basically what yacc/bison produces).
* Use Boost.Spirit (warning: this might make your head explode).

In every case, you need to write down a grammar that describes the expressions you want to parse.

If you give me some examples of the types of expressions you are trying to parse, I can help you with the process.

Share this post


Link to post
Share on other sites
I have solved the problem. No, sorry. I'm not writing a large parser. I'm just writing a small one that calculates math fractions. And thanks for helping. Here's my answer, and see if it's logical.

Let's say "|" is the bar you see in the middle of a fraction. Numerator is on the left, and denominator is on the right.

1|2 + 1|2 = 1|1;

That's the example. What I was doing, is using stacks and queues and be able to invoke a functions that does the math whenever the string iterator sees a specific operator sign. But...the initial problem is that at run-time, program can't detect the second fraction when I invoked the operator+ function for a given math problem.

When I incorperated the stacks concept into it, I used postfix, and do the math. By that, I was able to write and finish my program. Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this