function-pointers

Map Of functions c++

一世执手 提交于 2019-12-04 18:06:55
问题 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

Generating functions at runtime in C

谁说胖子不能爱 提交于 2019-12-04 13:49:03
问题 I would like to generate a function at runtime in C. And by this I mean I would essentially like to allocate some memory, point at it and execute it via function pointer. I realize this is a very complex topic and my question is naïve. I also realize there are some very robust libraries out there that do this (e.g. nanojit). But I would like to learn the technique, starting with the basics. Could someone knowledgeable give me a very simple example in C? EDIT: The answer below is great but

Function pointers to member functions of a parent class

两盒软妹~` 提交于 2019-12-04 13:11:39
I have been trying to understand function pointers in C++ so that I can successfully use them in one of my projects. I am running into a logic problem though. Say we have two classes: a parent class and a child class which inherits the parent class. class Parent{ ...other stuff void (Parent::*ptr2func) (); ...other stuff }; Then we have a child class: class Child : public Parent{ ...other stuff void afunc(); ...other stuff }; I wanted to connect the pointer of the parent class to the afunc() function of the child class. In the constructor of the child class is where I tried to do it like so:

Pointer to function to member function

≡放荡痞女 提交于 2019-12-04 12:26:32
I want to use a library (nlopt) that has a function set_min_objective which takes a pointer to a numerical function myfunc and find its minimum. I would like to create a class that will contain a suitably initialized member function. set_min_objective would then find an optimum in a specific instance (myP in the example below). The call sequence is: opt.set_min_objective(myfunc, NULL); and I would like to use something like: opt.set_min_objective(myP.f, NULL); the error I get when compiling this is: main.cpp: In function 'int main()': main.cpp:79:34: error: no matching function for call to

C++ member function pointer with different arguments - or is this bad anyway?

我怕爱的太早我们不能终老 提交于 2019-12-04 12:04:57
Even though I fear that you will tell me that this topic was covered several time, I dare to ask it, since I was not able to generate a solution. Probably I was just looking for the wrong thing... Assume that I have a function which receives a "mode" from some external function. Depending on the mode, the function will call different member functions of the same object. This works well for me with member function without any argument, but I did not find out how to extend it to members with arguments. In the real world application, the arguments are not int/float but a more complex classes and

NSComparisonResult and NSComparator - what are they?

岁酱吖の 提交于 2019-12-04 11:45:03
问题 What is NSComparisonResult and NSComparator ? I've seen one of the type definitions, something like that: typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); Is it any different from a function pointer? Also, I can't even guess what the ^ symbol means. 回答1: ^ signifies a block type , similar in concept to a function pointer. typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); // ^ ^ ^ // return type of block type name arguments This means that the type NSComparator is a

Calling a DLL function that contains a function pointer from C#

纵饮孤独 提交于 2019-12-04 10:37:59
I have a DLL that is written in C++ that includes exported function that has a function pointers to be used as callback function. // C++ DllExport unsigned int DllFunctionPointer( unsigned int i, unsigned int (*TimesThree)( unsigned int number ) ) { return TimesThree( i ) ; } I have a CSharp application that I would like to use to invoke the DLL functions. // C# public unsafe delegate System.UInt32 CallBack( System.UInt32 number ); class Program { [DllImport("SimpleDLL.dll")] public static extern System.UInt32 DllFunctionPointer( System.UInt32 i, CallBack cb) ; static unsafe void Main(string[]

map of pointers to functions of different return types and signatures

一曲冷凌霜 提交于 2019-12-04 09:51:30
I am looking for a way to call different functions by a string input. I have a map that ties each unique string to a function pointer and a lookup function to search the map and return a pointer if found. Now the trick is, I need a way to store and return pointers to functions with at least different return types, if possible, also with different signatures. The usage would be: Get a string input from a network socket -> find and execute the found function -> shove the result straight back into the socket to be serialized and sent, not caring what actually happened. Is this doable? If not, how

Dynamically creating a C++ function argument list at runtime

别说谁变了你拦得住时间么 提交于 2019-12-04 09:41:26
问题 I'm trying to generate an argument list for a function call during runtime, but I can't think of a way to accomplish this in c++. This is for a helper library I'm writing. I'm taking input data from the client over a network and using that data to make a call to a function pointer that the user has set previously. The function takes a string(of tokens, akin to printf), and a varying amount of arguments. What I need is a way to add more arguments depending on what data has been received from

Can a lambda expression be passed as function pointer?

我是研究僧i 提交于 2019-12-04 09:08:09
问题 I am trying to pass a lambda expression to a function that takes a function pointer, is this even possible? Here is some sample code, I'm using VS2010: #include <iostream> using namespace std; void func(int i){cout << "I'V BEEN CALLED: " << i <<endl;} void fptrfunc(void (*fptr)(int i), int j){fptr(j);} int main(){ fptrfunc(func,10); //this is ok fptrfunc([](int i){cout << "LAMBDA CALL " << i << endl; }, 20); //DOES NOT COMPILE return 0; } 回答1: In VC10 RTM, no - but after the lambda feature in