function-pointers

Passing any function as template parameter

自作多情 提交于 2019-12-02 17:29:17
I want to pass a function value as a template parameter to a function. Currently the best I managed to do is : template< typename F, F f > void pass() { ... } ...which is used: pass< decltype(&func), &func >(); What I would really like is to have: pass< &func >(); Is there any way to achieve this without macros? Basically to pass both the type and the value at the same time? The compiler obviously has all the information needed for that... The solution must work with variable parameters and return types. The function value is used at compile time, so it cannot be passed as an argument. C++11

Passing Python function to Boost C++

别来无恙 提交于 2019-12-02 17:23:17
问题 I'm trying to learn about Boost functions. I want to pass a Python function to a C++ module wrapped using Boost Python. I followed the example given here and modified it to accept functions that take an input argument and return some output. Here's my code: typedef double (op_t)(double); boost::function<op_t> op; double defaultOperator(double t) { return t; } void setOperator(boost::python::object obj) { op = obj; } double callOperator(double t) { return op(t); } BOOST_PYTHON_MODULE(op1) { op

Calling a member function using function pointers from another class's instance

馋奶兔 提交于 2019-12-02 17:00:45
问题 #include<iostream> #include<conio.h> using namespace std; class Base; typedef void (Base::*function)(); class Base { public: function f; Base() { cout<<"Base Class constructor"<<endl; } virtual void g()=0; virtual void h()=0; }; class Der:public Base { public: Der():Base() { cout<<"Derived Class Constructor"<<endl; f=(function)(&Der::g); } void g() { cout<<endl; cout<<"Function g in Derived class"<<endl; } void h() { cout<<"Function h in Derived class"<<endl; } }; class Handler { Base *b;

How does one declare an array of constant function pointers in C?

戏子无情 提交于 2019-12-02 15:16:10
I need to declare an array of pointers to functions like so: extern void function1(void); extern void function2(void); ... void (*MESSAGE_HANDLERS[])(void) = { function1, function2, ... }; However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do not recall where to place the const key-word(s). I'm assuming the actual pointer, MESSAGE_HANDLERS in this case, is already constant because it is declared as an array. On the otherhand, couldn't the function pointers within the array be change at runtime if it is declared

Meaning of int (*) (int *) = 5 (or any integer value)

寵の児 提交于 2019-12-02 14:25:10
I cannot figure this out: int main() { int (*) (int *) = 5; return 0; } The above assignment compiles with g++ c++11. I know that int (*) (int *) is a pointer to a function that accepts an (int *) as argument and returns an int, but I do not understand how you could equate it to 5. At first I thought it is a function that constantly returns 5 (from my recent learning in F#, probably, haha), then I thought, briefly, that the function pointer points to memory location 5, but that does not work, clearly, and neither does hex values. Thinking that it could be because the function returns an int,

Declaring a function pointer returning an array

假如想象 提交于 2019-12-02 14:16:17
For practice, I'm trying to : Declare fubar to be a pointer to a function that takes a pointer to a char and returns a pointer to an array of 24 elements where each element is a pointer to a struct foo . My logic is: -fubar is a pointer to a function taking a char pointer: (*fubar)(char*) -...returning a pointer to an array of 24 elems of where each elem is a struct foo: (struct foo *)(*fubar)(char*)[24] Is my logic correct? After fixing the syntax error and removing the parentheses around struct foo * , struct foo* (*fubar)(char*)[24] ...the one part that you got wrong is that it actually

struct pointer function points to other function of other struct

谁说胖子不能爱 提交于 2019-12-02 13:08:55
问题 I was wondering if is possible to point a function of other structure into of a structure: Example: typedef struct { int func(int z) { return z * 2; } } sta; typedef struct { int(*this.func)(int); } stah; int main() { sta sa; stah sah; sah.func = &sa.func; return 0; } it's possible this in a struct? 回答1: The declaration of func should look like this: int(sta::*func)(int); Or, alternatively: using my_type = int(sta::*)(int); my_type func; This is easier to read: my_type is an alias for the

Passing Python function to Boost C++

喜你入骨 提交于 2019-12-02 12:05:19
I'm trying to learn about Boost functions. I want to pass a Python function to a C++ module wrapped using Boost Python. I followed the example given here and modified it to accept functions that take an input argument and return some output. Here's my code: typedef double (op_t)(double); boost::function<op_t> op; double defaultOperator(double t) { return t; } void setOperator(boost::python::object obj) { op = obj; } double callOperator(double t) { return op(t); } BOOST_PYTHON_MODULE(op1) { op = &defaultOperator; def("setOperator", &setOperator); def("callOperator", &callOperator); } When I try

How to dynamically allocate function code?

不打扰是莪最后的温柔 提交于 2019-12-02 11:17:04
In the C language, the usual route for function pointers as callbacks from some library is to include a void* pointer for the context of the user: void (*fp)(void* ctx); The allows the library to call the callback with context ctx . Let's say I'm using a library that does not include a context pointer in the callbacks. I will need to have one callback for each context. What is the most portable way to dynamically allocate function pointers in C for providing callbacks? How can I malloc() a function code that can be called from the library? For example: typedef void (*my_fp_t)(char); my_fp_t fp

Vector of pointer to member functions

房东的猫 提交于 2019-12-02 10:49:12
I'm trying to write a program which creates a class that contains vector of pointers to member functions, with add() and remove() member functions. The code I wrote is - 1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 typedef void(*classFuncPtr)(); 6 7 class FunctionVectors 8 { 9 private: 10 vector<classFuncPtr> FunctionPointerVector; 11 public: 12 FunctionVectors(){} 13 void add(classFuncPtr funcPtr); 14 void remove(int index); 15 void run(); 16 void a(){cout<<"a: Why are you calling me?"<<endl;} 17 }; 18 19 void FunctionVectors::add(classFuncPtr funcPtr) 20 { 21