2

I program a Shell.

I have different functions with different parameters.

void func1(void);
void func2(char * x,);
void func3(char * x, string y);

At the moment a use a map to store string and pointer.

typedef map<string,void (*)()> t_list;
map<string,void(*)()>::iterator it;
t_list list;

list["argument1"]=&func1;

to find and start the function i use that:

it=list.find("argument");
if(it != list.end())
{
if->second();
}

Is it possible to store all functions with different parameters in the map? And how?

1
  • 2
    How do you intend to call these functions? Do you expect to be able to inspect their actual signatures and match them with provided arguments? Commented Mar 1, 2012 at 8:30

2 Answers 2

1

A reasonable solution for shell could be a function that takes a vector of strings and returns error code. So this single signature:

int (*func)(std::vector<std::string> const &args)

Then in the function you need to parse the arguments and you may call the real implementation (e.g. void func3(char * x, string y);)

Sign up to request clarification or add additional context in comments.

2 Comments

or maybe int (*func)(int argc, char* argv[]); =)
@wreckgar23: No, definitely not. That would be very unsafe.
0

Why don't you use functors? They are easier to use and they are more OOP link.

2 Comments

Probably because that will not solve his problem. Even functor need to have a defined signature if you want to store it in a map.
But you can use virtual functors, and then pass parameters as separate structure. That should solve the problem, am I right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.