Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a function which has it's arguments as char* or string,My problem is how to call a respective function using given string parameter without using string comparison methods.Is is efficient if we use switch-case statements as below:

C++
#include<string>
#include<iostream>
void foo(){ cout<<"foo";}

void bar(){cout<<"bar";}

void func_exe(string& s){

// I want to use this string parameter "s"/char* and execute respective function,without maps or string comparison,I want to use switch case  

}
void main(){

func_exe("foo");
func_exe("bar");

}


thank you
Posted
Updated 21-Oct-13 6:33am
v2
Comments
Captain Price 21-Oct-13 12:45pm    
A function does not take arguments as char* or string. It either takes char* or string. Do you want func_exe to work with both strings and char* s ? Or is it something else you are asking ?
Jochen Arndt 21-Oct-13 12:58pm    
The type of switch expressions must be integral (e.g. [unsigned] char, int, long). You can't use strings.
Captain Price 21-Oct-13 13:06pm    
Exactly :)
Sergey Alexandrovich Kryukov 21-Oct-13 13:16pm    
The whole thing makes no sense at all, even if a perfect solution existed. It would add negative value, not any gain.
Please see my answer.
—SA

couple of options :

1. have a if cascade on the input string to call the appropriate function
(pseudo-code)
if ( s == "foo" )
  foo();
else if ( s == "bar" )
  bar();
else
  cout<< "unsupported function";


2. have an map of function pointers (?) keyed to the "string" and call the appropriate function. (I'm no export on function pointers, so no pseudo-code )

I'm certain there are other ways to do it.

Good luck.
Max.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Oct-13 13:08pm    
I would say the whole idea is utterly bad, so this advice doesn't really worth the effort, sorry.
The function func_exe, even when it perfectly works, is really useless, or has purely negative value, as it is also not supportable.

Please see my comment to the question.
Such problems should not be "resolved" at all, you can only make things worse for the inquirers.

—SA
CPallini 21-Oct-13 13:17pm    
That makes perfect sense, my 5.
Buddhi Chaturanga 21-Oct-13 23:02pm    
Specific Problem like this,if you have an application(someone developed) input only taken as a String literal - "Fire" so when any user give the command "Fire" we have to execute the function Fire() in our app,so every relevant information/data we receive from that specific application are lying in string format think in real situation Function "Fire"-in C/C++ it defined like this:
void Fire(int x,int y);
then in specific app user input like this: Fire([23,45])
this command receive from my C/C++ application as a string "Fire([23,45])" therefore firstly I have to split this and put them as elements of vector<string>,so the first element of vector is Function name "Fire" others are arguments/parameters respectively.Using string "Fire" I have to execute the real Fire(x,y) Function,I prefer the most efficient manner which means without HASHMAPPING,ARRAY OF FUNCTIONS/FUNCTIONS POINTER,do we have any fundamental approach which is having simple mechanism?
Sergey Alexandrovich Kryukov 21-Oct-13 23:07pm    
First, please explain why such mechanism would be needed and how do you think it should be used. Or, rather, explain it first to yourself, maybe the results will be somewhat surprising.
—SA
The whole idea is totally useless. Think about the value you gain when you define the function func_exec. Imagine that it already works perfectly and can call any function without parameters without any need for support. Will it add any value to your code, in addition to the possibility to call foo, bar or anything else directly.

No. It will actually be a purely negative contribution, that is, it will make your code worse compared to the situation when you don't have this function. The call bar() will always be better then func_exec("bar"). Always, no exclusions. Much, much better. You cannot mistype the function, as the compiler will point out your error. But not in "bar", which you can misspell in any way.

Makes no sense, ever.

—SA
 
Share this answer
 
Comments
Captain Price 21-Oct-13 13:22pm    
"Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction." - Einstein. My 5 !
Sergey Alexandrovich Kryukov 21-Oct-13 13:51pm    
Thank you, Pravinda. Great quote, by the way, as well as the notion itself: "intelligent fool", so true.
—SA
Buddhi Chaturanga 21-Oct-13 23:02pm    
Specific Problem like this,if you have an application(someone developed) input only taken as a String literal - "Fire" so when any user give the command "Fire" we have to execute the function Fire() in our app,so every relevant information/data we receive from that specific application are lying in string format think in real situation Function "Fire"-in C/C++ it defined like this:
void Fire(int x,int y);
then in specific app user input like this: Fire([23,45])
this command receive from my C/C++ application as a string "Fire([23,45])" therefore firstly I have to split this and put them as elements of vector<string>,so the first element of vector is Function name "Fire" others are arguments/parameters respectively.Using string "Fire" I have to execute the real Fire(x,y) Function,I prefer the most efficient manner which means without HASHMAPPING,ARRAY OF FUNCTIONS/FUNCTIONS POINTER,do we have any fundamental approach which is having simple mechanism?
If you 'want to use the switch case' then you have to map the string to an integer, possibly creating the poor-man-hash-function, eventually you would mimic using a std::unordered_map. My advice is: directly use the std::unordered_map for the purpose.
 
Share this answer
 
Comments
Buddhi Chaturanga 21-Oct-13 23:02pm    
Specific Problem like this,if you have an application(someone developed) input only taken as a String literal - "Fire" so when any user give the command "Fire" we have to execute the function Fire() in our app,so every relevant information/data we receive from that specific application are lying in string format think in real situation Function "Fire"-in C/C++ it defined like this:
void Fire(int x,int y);
then in specific app user input like this: Fire([23,45])
this command receive from my C/C++ application as a string "Fire([23,45])" therefore firstly I have to split this and put them as elements of vector<string>,so the first element of vector is Function name "Fire" others are arguments/parameters respectively.Using string "Fire" I have to execute the real Fire(x,y) Function,I prefer the most efficient manner which means without HASHMAPPING,ARRAY OF FUNCTIONS/FUNCTIONS POINTER,do we have any fundamental approach which is having simple mechanism?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900