function-pointers

Exporting a function pointer from dll

爷,独闯天下 提交于 2019-12-20 04:46:05
问题 I have a function pointer in a dll file (in implementation, not header). How can I call the function pointed to by this pointer in the exe source code? 回答1: you can export a function returning the pointer. Header: typedef void ( *MyPtr ) (); __declspec( dllexport ) MyPtr GetMyPtr(); Source: MyPtr GetMyPtr() { //retunr the function pointer here } 来源: https://stackoverflow.com/questions/7764074/exporting-a-function-pointer-from-dll

Bubble sort universal implementation in C

耗尽温柔 提交于 2019-12-20 02:26:22
问题 I'm trying to make an universal bubble sort function. It allow to user to write its own compare and swap function. I implemented a swap and compare function for int type, but when I run the code for next array: {3, 5, 8, 9, 1, 2, 4, 7, 6, 0} , I get: 0 0 84214528 2312 1 2 4 7 6 0. Why this is happening? #include <stdio.h> #include <stdlib.h> #define true 1 #define false 0 int compInt(void *a, void *b) // FUNCTION FOR COMPARE INT { if (*(int*)(a) > *(int*)(b)) { return false; } // IF FIRST INT

Misunderstanding function pointer - passing it as an argument

烈酒焚心 提交于 2019-12-19 21:08:30
问题 I want to pass a member function of class A to class B via a function pointer as argument. Please advise whether this road is leading somewhere and help me fill the pothole. #include <iostream> using namespace std; class A{ public: int dosomeA(int x){ cout<< "doing some A to "<<x <<endl; return(0); } }; class B{ public: B(int (*ptr)(int)){ptr(0);}; }; int main() { A a; int (*APtr)(int)=&A::dosomeA; B b(APtr); return 0; } This brilliant piece of code leaves me with the compiler error: cannot

Unordered map: issue using class member function pointer

丶灬走出姿态 提交于 2019-12-19 11:58:08
问题 I have the following problem: I am writing a simple chip8 emulator, and have a massive class of interpreter functions that I would like to access via opcodes as keys, such as with a dictionary. That is to replace a massive switch case, and I understand that for that purpose, an unordered map is a nice tool to use. This approach works easily with just functions (because of their static scope, I assume), but does not with classes, because of the same concept of scope. I am somewhat new to

How to make template parameter

一个人想着一个人 提交于 2019-12-19 10:54:36
问题 How do I create a metafunction that takes any kind of function pointer? In the code below, how do I get rid of "decltype(&f)" ? template <class FuncType, FuncType functionPointer> void runFunc() { functionPointer(); } runFunc<decltype(&f),f>(); I don't want to have to specify the type of f seperately; the information is already there in f. I'd prefer not to resort to defines to solve this. This is basically the templated function type idiom applied to meta-programming; I don't want to know

How could I pass std::function as function pointer?

↘锁芯ラ 提交于 2019-12-19 10:21:17
问题 I am trying to write a class template and internally it use a C function (implementation of BFGS optimization, provided by the R environment) with the following interface: void vmmin(int n, double *x, double *Fmin, optimfn fn, optimgr gr, ... , void *ex, ... ); where fn and gr are function pointers of type typedef double optimfn(int n, double *par, void *ex); and typedef void optimgr(int n, double *par, double *gr, void *ex); respectively. My C++ class template looks like this: template

Implicit cast from function to function pointer?

有些话、适合烂在心里 提交于 2019-12-19 09:44:37
问题 I have a function that accepts as an argument a function pointer. Surprisingly, I can pass in both a function pointer and an ordinary function: #include <iostream> #include <functional> int triple(int a) { return 3*a; } int apply(int (*f)(int), int n) { return f(n); } int main() { std::cout << apply(triple, 7) << "\n"; std::cout << apply(&triple, 7) << "\n"; } I'm confused about why this works. Is there an implicit conversion from functions to function pointers? 回答1: Yes, there's function-to

How to pass a function pointer to a function with variable arguments?

£可爱£侵袭症+ 提交于 2019-12-19 07:10:12
问题 I don't know how to accomplish this! how to get the function pointer in va_list arguments? thanks so much. 回答1: Use a typedef for the function pointer type. 回答2: Typedefs often make working with function pointers easier, but are not necessary. #include <stdarg.h> void foo(int count, ...) { va_list ap; int i; va_start(ap, count); for (i = 0; i < count; i++) { void (*bar)() = va_arg(ap, void (*)()); (*bar)(); } va_end(ap); } 来源: https://stackoverflow.com/questions/1596476/how-to-pass-a-function

How to pass a function pointer to a function with variable arguments?

让人想犯罪 __ 提交于 2019-12-19 07:10:04
问题 I don't know how to accomplish this! how to get the function pointer in va_list arguments? thanks so much. 回答1: Use a typedef for the function pointer type. 回答2: Typedefs often make working with function pointers easier, but are not necessary. #include <stdarg.h> void foo(int count, ...) { va_list ap; int i; va_start(ap, count); for (i = 0; i < count; i++) { void (*bar)() = va_arg(ap, void (*)()); (*bar)(); } va_end(ap); } 来源: https://stackoverflow.com/questions/1596476/how-to-pass-a-function

Emulating std::bind in C

纵饮孤独 提交于 2019-12-19 06:45:11
问题 I'm using std::bind to provide a callback while abstracting some logic by binding some parameters first. i.e. void start() { int secret_id = 43534; //Bind the secret_id to the callback function object std::function<void(std::string)> cb = std::bind(&callback, secret_id, std::placeholders::_1); do_action(cb); } void do_action(std::function<void(std::string)> cb) { std::string result = "hello world"; //Do some things... //Call the callback cb(result); } void callback(int secret_id, std::string