Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Of functions c++

I have made a map of functions. all these functions are void and receive single string parameter.

code:

void f1(string params){...}
void f2(string params){...}
void f3(string params){...}

map<string , void*> funcMap;

funcMap["f1"] =(void*)&f1;
funcMap["f2"] =(void*)&f2;
funcMap["f3"] =(void*)&f3;

how do i call a function? I tried the next code, but id doesn't work:

void (*func)(string) =  &funcMap[commandType];
func(commandParam);

I get this error message:

Server.cpp:160:46: error: cannot convert ‘void**’ to ‘void (*)(std::string) {aka void (*)(std::basic_string<char>)}’ in initialization
like image 549
kakush Avatar asked Jun 14 '12 19:06

kakush


People also ask

What is a map function in C?

The map function is commonly used as a built-in Arduino C function and it's very handy in a wide range of applications. Mathematically, the mapping function is to find the corresponding value from a certain domain to another domain.

Is there map in C?

The C Programming Language by Kernighan and Ritchie has an example of making an associate map in c, and what I'll detail below is based on what I remember from that. Basically you'll need a struct Map that contains struct Key and struct Value.

What are two functions of a map?

What is the Purpose of a Map? Maps serve two map functions; they are a spatial database and a communication device. The science of making maps is called cartography. Basic map characteristics tell the reader where an object is (location) and what the object is (its attributes).

What is the map () function and how does it work?

Definition and Usage map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.


3 Answers

using pfunc = void (*)(string);

map<string, pfunc> funcMap; 
funcMap["f1"] = f1; //and so forth

And then call:

pfunc f = funcMap[commandType];
(*f)(commandParam);   

In general, why throw away type safety? If it's a map of function pointers, declare it to be one.

like image 189
Seva Alekseyev Avatar answered Oct 27 '22 19:10

Seva Alekseyev


Why not just have those as separate classes.

Then have the methods as virtual.

You can then have a map between the string and the base class.

i.e.

class Someoperation
{
    virtual void Doit() = 0;
};

map<string, Someopertion> ops;

Then

class MyOp : public Someoperation
{
   void Doit() { /* Some code here */}
};

Just add objects

ops["Hello"] = MyOp();

then call it

ops["Hello"].Doit();
like image 28
Ed Heal Avatar answered Oct 27 '22 19:10

Ed Heal


&funcMap[commandType]

Just drop the &. Your compile error was useful here. It had a void** on the right which is because you took the address of a function pointer. You don't want two levels of indirection there.

like image 22
djechlin Avatar answered Oct 27 '22 17:10

djechlin