function-pointers

Generic binary search tree in C

白昼怎懂夜的黑 提交于 2019-12-01 06:02:17
问题 I have the implemented a binary search tree but I also want to make it generic. The code is the following: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode; and the functions: treeNode* FindMin(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->left) /* Go to the left sub tree to find the min element */ return FindMin(node->left); else return node; } treeNode * Insert(treeNode *node,int data) { if(node=

Is it possible to replace a method at runtime?

▼魔方 西西 提交于 2019-12-01 05:32:17
问题 I want to make a plugin system with the capability to override a method at runtime. Some answers say function pointers, but how about a defined function or class? Like this: class foo { public: bar(int foobar); } Is there a way to get function pointer for that, or replace it? BTW, hooking is not considered an answer because it's very platform specific and dangerous. 回答1: Runtime function "replacement" can be achieved with one of several techniques: Polymorphism Standard library facilities

Emulating std::bind in C

最后都变了- 提交于 2019-12-01 04:41:39
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 result) { //Callback can now do something with the result and secret_id } So in the above example, the

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

寵の児 提交于 2019-12-01 04:33:52
I don't know how to accomplish this! how to get the function pointer in va_list arguments? thanks so much. Use a typedef for the function pointer type. 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-pointer-to-a-function-with-variable-arguments

Python ctypes: How to modify an existing char* array

删除回忆录丶 提交于 2019-12-01 04:13:25
I'm working on a Python application that makes use of libupnp which is a C library. I'm using CTypes to use the library which is easy enough. The problem I'm having is when I'm registering a callback function for read requests. The function has a prototype of the following form: int read_callback(void *pFileHandle, char *pBuf, long nBufLength); pFileHandle is just some file handle type. pBuf is a writable memory buffer. This is where the data is output. nBufLength is the number of bytes to read from the file. A status code is returned. I have a Python function pointer for this. That was easy

To pass a pointer to a member function

家住魔仙堡 提交于 2019-12-01 03:43:12
问题 I have an class with instance functions (or methods?). From within an instance, I try to pass pointers to those functions to a library. The library expects static functions. When I pass my pointers to the callback functions, the compiler complains that my functions are not static. I tried to put make them static, but if I do so, then I can't access the instance fields from within the functions. How could I go around this? Similar question is : Using a C++ class member function as a C callback

Visual C++ ~ Not inlining simple const function pointer calls

徘徊边缘 提交于 2019-12-01 03:38:07
Dear StackOverflowers, I got a simple piece of code which I am compiling on Microsoft Visual Studio C++ 2012: int add(int x, int y) { return x + y; } typedef int (*func_t)(int, int); class A { public: const static func_t FP; }; const func_t A::FP = &add; int main() { int x = 3; int y = 2; int z = A::FP(x, y); return 0; } The compiler generates the following code: int main() { 000000013FBA2430 sub rsp,28h int x = 3; int y = 2; int z = A::FP(x, y); 000000013FBA2434 mov edx,2 000000013FBA2439 lea ecx,[rdx+1] 000000013FBA243C call qword ptr [A::FP (013FBA45C0h)] return 0; 000000013FBA2442 xor eax

static constexpr pointer-to-function, difference between compilers

左心房为你撑大大i 提交于 2019-12-01 03:34:28
When answering this question , I tried the following code with gcc ( code compiled ) and clang ( code rejected ): typedef long (*func)(int); long function(int) { return 42; } struct Test { static constexpr func f = &function; }; template<func c> struct Call { static void f() { c(0); } }; int main() { Call<Test::f>::f(); } I am not sure which compiler is right, although I think the constexpr initialization of Test::f is ok. The error clang outputs is: error: non-type template argument for template parameter of pointer type 'func' (aka 'long (*)(int)') must have its address taken Which compiler

Why can't one compare a function pointer to a template function without explicit & on function name?

好久不见. 提交于 2019-12-01 03:01:27
Consider the following code: void func(int) {} template<typename T> void templatedFunc(T) {} int main() { void (*p)(int) = func; bool test1 = p==func; //bool test2 = p==templatedFunc<int>; // compilation error bool test3 = p==&templatedFunc<int>; // but this works } If you uncomment the test2 line and try to compile the code with g++, you'll get the following error: test.cpp: In function ‘int main()’: test.cpp:8:21: error: assuming cast to type ‘void (*)(int)’ from overloaded function [-fpermissive] bool test2 = p==templatedFunc<int>; // compilation error ^~~~~~~~~~~~~~~~~~ I get this result

Defining a delegate as a function pointer

不打扰是莪最后的温柔 提交于 2019-12-01 02:58:20
问题 I am using a delegate which calls an unmanaged function pointer. This causes the Garbage Collector to collect it before it is used, as described in the CallbackOnCollectedDelegate MDA page on MSDN: MSDN page for CallbackOnCollectedDelegate MDA. The resolution states that I have to marshal the appropriate delegate as an unmanaged function pointer. My initial reflex was to use: [MarshalAs(UnmanagedType.FunctionPtr)] public delegate void EntityCallback([MarshalAs(UnmanagedType.SysInt)] IntPtr