kakush
kakush

Reputation: 3484

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

Upvotes: 16

Views: 31804

Answers (4)

snaiffer
snaiffer

Reputation: 720

Try C++ style. It has overhead for allocation and inheritance, but it's more flexible and extensible if you'll need some more functionality in the future.

#include <iostream>
#include <string>
#include <unordered_map>
#include <memory>
using namespace std;

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

class MyOp1 : public Someoperation {
public:
    void Doit() final { cout << "MyOp1" << endl; }
};

class MyOp2 : public Someoperation {
public:
    void Doit() final { cout << "MyOp2" << endl; }
};

int main() {
    unordered_map<string, unique_ptr<Someoperation> > ops;

    ops["1"] = unique_ptr<Someoperation>(new MyOp1);
    ops["2"] = unique_ptr<Someoperation>(new MyOp2);
    ops["1"]->Doit();   // Out: MyOp1
    ops["2"]->Doit();   // Out: MyOp2

    return 0;
}

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

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.

Upvotes: 21

Ed Heal
Ed Heal

Reputation: 59997

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();

Upvotes: 2

djechlin
djechlin

Reputation: 60758

&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.

Upvotes: 1

Related Questions