function-pointers

Create thread is not accepting the member function

↘锁芯ラ 提交于 2019-11-30 07:49:28
问题 I am trying to create a class for network programming. This will create a general purpose socket with thread. But when I tried to crete the thread using createthread(). The third argument is producing errors. And from the net I came to know that I can't use the member functions as an argument to the createthread(). Is there any thing by which I can achieve this? 回答1: The easiest way to handle this is to create a "stub" function which calls back into your class. UINT tid HANDLE hThread =

Store Function Pointers to any Member Function

空扰寡人 提交于 2019-11-30 07:43:16
My Event Manager For a event manager I need to store many pointers to functions in a vector to call them when the event is triggered. (I will provide the source code of the EventFunction helper class at the end of this question.) // an event is defined by a string name and a number typedef pair<string, int> EventKey; // EventFunction holds a pointer to a listener function with or without data parameter typedef unordered_map<EventKey, vector<EventFunction>> ListEvent; // stores all events and their listeners ListEvent List; Registering an listener could be done by calling the first or the

Is it safe to cast a lambda function to a function pointer?

霸气de小男生 提交于 2019-11-30 07:27:29
问题 I have this code: void foo(void (*bar)()) { bar(); } int main() { foo([] { int x = 2; }); } However, I'm worried that this will suffer the same fate as: struct X { int i; }; void foo(X* x) { x->i = 2; } int main() { foo(&X()); } Which takes the address of a local variable. Is the first example completely safe? 回答1: Yes I believe the first example is safe, regardless of the life-time of all the temporaries created during the evaluation of the full-expression that involves the capture-less

Implementing a generical 'map' function over arrays in C

痞子三分冷 提交于 2019-11-30 07:07:32
I'm having difficulties implementing a generic 'map' function over arrays. I started with the following draft: void MapArray(void * src, void * dest, void * (f)(void *), size_t n, size_t elem) { unsigned int i = 0, j = 0; void * temp = malloc(elem); for(i = 0; i<n, i++) { temp = (f)((char *) src) + i)); for(j = 0; j < elem; j++) { *(((char *) dest) + i) = *(((char *) temp) + i); } } free(temp); } I understand why it's not correct - i'm casting to (char *) before giving it to 'f' - but i'm now demotivated and can't come up with a solution. (I'm doing this in the process of learning C) My

If ampersands aren't needed for function pointers, why does boost::bind require one?

自古美人都是妖i 提交于 2019-11-30 06:56:00
I've always believed that function pointers don't require an ampersand: Do function pointers need an ampersand Yet, every example I've seen of using boost::bind shows one, and my compiler - in most situations - gives a typically inscrutable error message if it's omitted. synchronize(boost::bind(&Device::asyncUpdate , this, "ErrorMessage")); // Works synchronize(boost::bind(Device::asyncUpdate , this, "ErrorMessage")); // Fails Am I wrong in assuming that boost::bind 's first parameter is basically function pointer? Function pointers don't need it, member function pointers do. Device:

Class member function pointer

和自甴很熟 提交于 2019-11-30 06:25:59
问题 I'm trying to use a class function (interrupt service routine), void (ClassName::*fp)(void)=ClassName::FunctionName; and attaching it to an Arduino interrupt pin using the function with the following type inputs but that doesn't work. void attachInterrupt(int, void (*)(void),int); How can I make this happen? The interrupt service routine (ISR) needs to access privat object data, so I can't make a function outside of the class. My compiler error: ClassName.cpp : : In constructor 'ClassName:

How to define typedef of function pointer which has template arguments

你。 提交于 2019-11-30 06:23:27
问题 I would like to make typedef for function pointer which has stl container as argument and this container has unknown type. Something like this: typedef void (* TouchCallBack)(GLRenderer*, const MotionEvent&, std::vector<T >); it's possible? (especially in c++ 03) 回答1: I don't know of any C++03 solution exactly like that, and it's not built into the language, but in C++11, this is possible with using aliases: template<typename T> using TouchCallBack = void (*)(GLRenderer*, const MotionEvent&,

How does the C code that prints from 1 to 1000 without loops or conditional statements work?

不羁岁月 提交于 2019-11-30 06:08:16
问题 I've found C code that prints from 1 to 1000 without loops or conditionals : But I don't understand how it works. Can anyone go through the code and explain each line? #include <stdio.h> #include <stdlib.h> void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } 回答1: Don't ever write code like that. For j<1000 , j/1000 is zero (integer division). So: (&main + (&exit - &main)*(j/1000))(j+1); is equivalent to: (&main + (&exit - &main)*0)(j+1); Which is: (&main)(j+1);

C: How can I use a single function pointer array for functions with variable parameter counts?

南笙酒味 提交于 2019-11-30 05:06:35
The question pretty much says it all. I'm not sure how to do this and haven't come anywhere near anything that works. Here's some example functions: add(int x, int y) { return x+y; } and, mean(int x1, int y1, int x2, int y2) { return (x1 + y1 + x2 + y2) / 4; } So far I've tried using typedef with both, but I can't figure how to make something point to one of either type: typedef int (*mathfunc2)(int x, int y); typedef int (*mathfunc4)(int x1, int y1, int x2, int y2); ????? func_table[2] = {add, mean}; You need to pick a function pointer type to use as a "generic function pointer", use that

Cast to function pointer?

天涯浪子 提交于 2019-11-30 04:38:28
Have come across the line of code shown below I think it may be a cast to a function pointer that returns void and takes a void pointer, is that correct? (void (*)(void *))SGENT_1_calc Yes it is correct. I find that not very readable, so I suggest declaring the signature of the function to be pointed: typedef void sigrout_t(void*); I also have the coding convention that types ending with rout_t are such types for functions signatures. You might name it otherwise, since _t is a suffix reserved by Posix latter on I am casting, perhaps to call it like ((sigrout_t*) SGENT_1_calc) (someptr); Yes it