function-pointers

How do function pointers work?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 03:46:04
问题 I'm asking some specific questions. How can I initialize them in a class? How can I pass a function as an argument? Do function pointers need to be declared and defined in the class? For question number 2 here is what I mean: void s(void) { //... } void f(function) { // what should I put as type to pass a function as an argument //... } f(s); 回答1: To define a function pointer, use the following syntax: return_type (*ref_name) (type args, ...) So, to define a function reference named

C++ How to Reference Templated Functions using std::bind / std::function

三世轮回 提交于 2019-11-30 03:29:19
If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)? I was given some help about the basic syntax in a post below, to bind to functions with explicit template type parameters, but lose the ability to provide template type parameters in the process. Is it possible to get this to work so that it is still possible to provide template type parameters with future calls? Cleaned up this code a lot, but it obviously won't compile because I can't find the correct syntax, (are there any ways of doing

If-statement vs function pointer

旧城冷巷雨未停 提交于 2019-11-30 03:12:21
问题 The goal is to change the behaviour in an event loop, depending on whether a checkbox is toggled on or off. The simplest way, that I can think of, is just to test the checkbox state each time the loop is run. // if-statement void action() { /* ... */ } void someLoop() { if (checkboxTrue) { action(); } // ... other stuff } Would the code be more performant and cleaner or in any other way better, if a function pointer was being used? Like this: // function pointer void action() { /* ... */ }

Do compilers optimize away calls to trivial functions made through pointers?

北城以北 提交于 2019-11-30 01:24:34
问题 Say I have a function that takes a function pointer: int funct(double (*f)(double)); And I pass it a function that doesn't actually do anything: double g(double a) { return 1.0;} //... funct(g); Will the compiler optimize out the calls to g ? Or will this still have overhead? If it does have overhead, how much? Enough that it is worth overloading the function to receive both function pointers and constant values? 回答1: Newer versions of GCC (4.4 and later) can inline and optimize a known

“this” pointer in C (not C++)

末鹿安然 提交于 2019-11-30 00:36:01
I'm trying to create a stack in C for fun, and came up with the idea of using struct to represent the stack. Then I add function pointers to the struct for push() and pop() operations. So far all is good it seems, but, for the implementation of the push() and pop() functions I need to refer to *this somehow. How can that (can it?) be done? This is my struct struct Stack { int *data; int current_size; int max_size; int (*push)(int); int (*pop)(); }; And as an example here's push int push(int val) { if(current_size == max_size -1) return 0; data[current_size] = val; current_size++; return 1; }

The Benefits of Using Function Pointers

谁都会走 提交于 2019-11-29 21:54:10
I have been programming for a few years now and have used function pointers in certain cases. What I would like to know is when is it appropriate or not to use them for performance reasons and I mean in the context of games, not business software. Function pointers are fast, John Carmack used them to the extent of abuse in the Quake and Doom source code and because he is a genius :) I would like to use function pointers more but I want to use them where they are most appropriate. These days what are the best and most practical uses of function pointers in modern c-style languages such as C, C+

calling code stored in the heap from vc++

扶醉桌前 提交于 2019-11-29 20:31:23
Imagine I am doing something like this: void *p = malloc (1000); *((char*)p) = some_opcode; *((char*)p+1) = another_opcode; // for the sake of the example: the opcodes are ok .... etc... How can I define a function pointer to call p as if it was a function? (i'm using VC++ 2008 express). Thanks A comment wasn't enough space. Joe_Muc is correct. You should not stuff code into memory obtained by malloc or new . You will run into problems if you change the page properties of pages that Windows allocates. This isn't a problem becuase using VirtualAlloc() and the related WIn32 APIs is every easy:

Function pointer as parameter

给你一囗甜甜゛ 提交于 2019-11-29 19:25:37
I try to call a function which passed as function pointer with no argument, but I can't make it work. void *disconnectFunc; void D::setDisconnectFunc(void (*func)){ disconnectFunc = func; } void D::disconnected(){ *disconnectFunc; connected = false; } The correct way to do this is: typedef void (*callback_function)(void); // type for conciseness callback_function disconnectFunc; // variable to store function pointer type void D::setDisconnectFunc(callback_function pFunc) { disconnectFunc = pFunc; // store } void D::disconnected() { disconnectFunc(); // call connected = false; } Replace void

How can I create function pointers from a string input in MATLAB?

自古美人都是妖i 提交于 2019-11-29 18:36:16
问题 If I use the inline function in MATLAB I can create a single function name that could respond differently depending on previous choices: if (someCondition) p = inline('a - b','a','b'); else p = inline('a + b','a','b'); end c = p(1,2); d = p(3,4); But the inline functions I'm creating are becoming quite epic, so I'd like to change them to other types of functions (i.e. m-files, subfunctions, or nested functions). Let's say I have m-files like Mercator.m , KavrayskiyVII.m , etc. (all taking a

CUDA: Copy dynamically created array of function pointers on the CPU to GPU memory

跟風遠走 提交于 2019-11-29 18:14:55
I would like to create a list of function pointers dynamically on the CPU (with some sort of push_back() method called from main() ) and copy it to a GPU __constant__ or __device__ array, without needing to resort to static __device__ function pointers. I believe this question is related to my problem; however, my goal is to create the __host__ function pointer array iteratively and then copy it to the __constant__ function pointer array instead of initialising the latter on declaration. A working code example with static function pointers (as seen here or here ) would be: common.h: #ifndef