function-pointers

C++ Pointer to member function of an UNKNOWN CLASS

大憨熊 提交于 2019-12-06 07:30:13
DISCLAIMER I DO NOT USE BOOST OR OTHER LIBRARIES Finally I've learned how PointerToMemberFunction works. This is my example code . #include <iostream> using namespace std; class Foo { public: void foo ( ) { cout << "I'm a foo method\n"; }; }; class Bar { public: void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() ) { (fooPtr->*fooFnPtr)(); }; }; int main() { Foo* foo = new Foo(); Bar* bar = new Bar(); bar->bar ( foo , &Foo::foo ); return 0; } Now, what the problem is. Bar::bar must be modified somehow, because in real project it won't know, what class fooFnPtr is a pointer to . In other words Bar:

calling functions returned as pointers from other functions in ctypes

社会主义新天地 提交于 2019-12-06 07:25:58
I'm trying to use ctypes to call a c function that was returned as a pointer from another function. It seems from the documentation that I can do this by declaring the function with CFUNCTYPE , and then creating an instance using the pointer. This, however seems to give me a segfault. Here is some sample code. sample.c: #include <stdio.h> unsigned long long simple(void *ptr) { printf("pointer = %p\n", ptr); return (unsigned long long)ptr; } void *foo() { return (void *)simple; } unsigned long long (*bar)(void *ptr) = simple; int main() { bar(foo()); simple(foo()); } and simple.py: from ctypes

Function pointers to member functions of a parent class

六月ゝ 毕业季﹏ 提交于 2019-12-06 06:29:46
问题 I have been trying to understand function pointers in C++ so that I can successfully use them in one of my projects. I am running into a logic problem though. Say we have two classes: a parent class and a child class which inherits the parent class. class Parent{ ...other stuff void (Parent::*ptr2func) (); ...other stuff }; Then we have a child class: class Child : public Parent{ ...other stuff void afunc(); ...other stuff }; I wanted to connect the pointer of the parent class to the afunc()

Cython: How to expose void* and function pointer in struct?

霸气de小男生 提交于 2019-12-06 06:15:33
I have a C header with: typedef struct { <normal members> void (*cb_func)(glp_tree *T, void *info); void *cb_info; <normal members> } glp_iocp; Currently, in my pxd file: ctypedef struct IntOptCP "glp_iocp": <normal members> int out_dly # mip.out_dly (milliseconds) #void (*cb_func)(Tree* tree, void* info) # mip.cb_func #void* cb_info # mip.cb_info <normal members> In a pyx file, at some point, I do (essentially): cdef class MyClass: IntOptCP _iocp <__cintit__ and the like> def some_method(self): <manipulation of self._iocp> controls = dict() controls = self._iocp return controls This works

map of pointers to functions of different return types and signatures

允我心安 提交于 2019-12-06 05:52:42
问题 I am looking for a way to call different functions by a string input. I have a map that ties each unique string to a function pointer and a lookup function to search the map and return a pointer if found. Now the trick is, I need a way to store and return pointers to functions with at least different return types, if possible, also with different signatures. The usage would be: Get a string input from a network socket -> find and execute the found function -> shove the result straight back

g++ -Waddress may misunderstand my meaning

試著忘記壹切 提交于 2019-12-06 04:13:00
The example code is: #include <iostream> using std::cout; using std::endl; void bar(double *) { cout << "call bar()" << endl; } using Bar = void(*)(double *); template <Bar pfunction> void foo() { // when call "foo<bar>()", there is a warning: // the address of ‘void bar(double*)’ will never be NULL [-Waddress] if (nullptr != pfunction) { pfunction(nullptr); } cout << "shit" << endl; } int main() { foo<nullptr>(); // OK foo<bar>(); // warning return 0; } from gcc manual: -Waddress Warn about suspicious uses of memory addresses. These include using the address of a function in a conditional

C++ function pointer to a member function - which address does it receive?

烈酒焚心 提交于 2019-12-06 03:22:23
Assuming I have this class: class Shape { public: int value; Shape(int v) : value(v) {}; void draw() { cout << "Drawn the element with id: " << value << endl; } }; and the following code (which works) Shape *myShapeObject = new Shape(22); void (Shape::*drawpntr)(); drawpntr = &Shape::draw; (myShapeObject ->*drawpntr)(); I have a drawpntr function pointer to a void-returning 0-arguments function member of the class Shape. First thing I'd like to ask: drawpntr = &Shape::draw; the function is a member function and there's no object here.. what address does drawpntr receive? The class shouldn't

Why isn't a lambda that captures variables by reference convertible to a function pointer?

心已入冬 提交于 2019-12-06 03:10:51
问题 If I have a lambda which captures all automatic variables by reference ( [&] {} ), why can't it be converted to a function pointer? A regular function can modify variables just like a lambda that captures everything by reference can, so why is it not the same? I guess in other words, what is the functional difference between a lambda with a & capture list and a regular function such that the lambda is not convertible to a function pointer? 回答1: So let's take the example of a trivial lambda:

Using non-const expression as template parameter

非 Y 不嫁゛ 提交于 2019-12-06 02:35:43
This is a follow up on How do I get the argument types of a function pointer in a variadic template class? I have this struct to access the arguments of a variadic template: template<typename T> struct function_traits; template<typename R, typename ...Args> struct function_traits<std::function<R(Args...)>> { static const size_t nargs = sizeof...(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; }; }; And I access the type of an argument of Args with typedef function<void(Args...)> fun; std::cout << std::is

pointer to const vs usual pointer (for functions)

荒凉一梦 提交于 2019-12-06 02:35:13
问题 Is there any difference between pointer to const and usual pointer for functions? When it is suitable to use const qualifier for stand alone functions? I wrote short sample to illustrate my question: #include <iostream> using namespace std; int sum( int x, int y ) { return x + y; } typedef int sum_func( int, int ); int main() { const sum_func* sum_func_cptr = ∑ // const function sum_func* sum_func_ptr = ∑ // non-const function ? // What is the difference between sum_func_cptr and sum_func_ptr