function-pointers

non-member function pointer as a callback in API to member function

允我心安 提交于 2019-12-05 18:52:14
I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class in C++ but I'm getting compilation errors. The API definition is: typedef void (__stdcall *STREAM_CALLBACK)(void *userdata); __declspec(dllimport) int __stdcall set_stream_callback( STREAM_CALLBACK streamCB, void *userdata); One example file, provided by the third party, is: void __stdcall streamCB(void *userdata) { // callback implementation } int main(int argc, const char argv[]) { int mid = 0; set_stream_callback(streamCB, &mid); } And that works fine. However when I try to

Accessing function pointer inside class

你。 提交于 2019-12-05 18:14:07
I am defining function pointer inside a class and trying to access it through an instance of the class but it shows an error. Here is the code: 1 #include<stdio.h> 2 3 class pointer { 4 public: 5 int (pointer::*funcPtr)(int); 6 pointer() { 7 funcPtr = &pointer::check; 8 } 9 10 11 int check(int a) 12 { 13 return 0; 14 } 15 16 }; 17 18 int main() 19 { 20 pointer *pt=new pointer; 21 return (pt->*funcPtr)(3); 22 } It shows a compile time error: checkPointer.cpp:21:15: error: ‘funcPtr’ was not declared in this scope please help me. Thank You in advance. The issue here is that funcPtr is declared

Function pointer with undetermined parameter

余生颓废 提交于 2019-12-05 17:58:23
I want to have a function pointer that can take various types of parameters. How do I do that? The following example (line 1), I want void (*fp)(int) to be able to take void (*fp)(char*) as well. The following code does not properly compile because I'm passing char* where int is expected so compiling the following code will give you warnings (and won't work properly). void callIt(void (*fp)(int)) { (*fp)(5); } void intPrint(int x) { printf("%d\n", x); } void stringPrint(char *s) { printf("%s\n", s); } int main() { void (*fp1)(int) = intPrint; callIt(fp1); void (*fp2)(char*) = stringPrint;

“Converting” a function pointer to a block in objective-C

强颜欢笑 提交于 2019-12-05 17:36:56
问题 I'm doing some Interop from Mono C# to Obj-C and ran into this problem. The C# code needs to pass a callback - which it does with a function pointer. I can get the function pointer from the Obj-C side and call it and everything works. But I now need to give that function pointer as a callback to third party API which works with blocks as a callback. I want the third party to call the C# function - so in a way i'm trying to either convert the function pointer to a block so the third party can

Why can function pointers be used with or without the address of operator? [duplicate]

*爱你&永不变心* 提交于 2019-12-05 17:34:47
This question already has an answer here: Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'? 2 answers In the book, "Beginning C from Novice to Professional", the author does not use the address of operator when assigning a function to a function pointer. I typed in the code on my compiler both with and without the address of operator and it compiled and performed as expected both times. Why is this and which way would be preferred in an enterprise/business setting? int sum(int, int); int main(void) { ... int (*pfun)(int, int); pfun = ∑ pfun = sum; ...

How do you declare an extern “C” function pointer

落爺英雄遲暮 提交于 2019-12-05 17:34:30
问题 So I have this code: #include "boost_bind.h" #include <math.h> #include <vector> #include <algorithm> double foo(double num, double (*func)(double)) { return 65.4; } int main(int argc, char** argv) { std::vector<double> vec; vec.push_back(5.0); vec.push_back(6.0); std::transform(vec.begin(), vec.end(), vec.begin(), boost::bind(foo, _1, log)); } And receive this error: return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]); .....................................................

Is there such a thing as a generic function pointer in C that can be assigned/casted to a more restrictive prototype?

家住魔仙堡 提交于 2019-12-05 14:50:41
问题 I have the need to dynamically link against a library at run-time and resolve a series of functions using dlsym . My first thought was to use an array of function pointers that can be easily iterated through by leveraging a secondary array of char * representing the symbol names. However, the problem with this is that not all of the functions take the same arguments. Is there a way to use a generic function pointer in the array, but assign it to a more restrictive function pointer prototype?

Non-type template function pointer to const member function

心已入冬 提交于 2019-12-05 14:38:11
I'm writing a delegate class but it fails to take const member functions. Here is a test case : class foo { public: void MemberFunction() { printf("non const member function\n"); } void ConstMemberFunction() const { printf("const member function\n"); } }; template <class C, void (C::*Function)()> void Call(C* instance) { (instance->*Function)(); } int main (int argc, char** argv) { foo bar; Call<foo,&foo::MemberFunction>(&bar); Call<foo,&foo::ConstMemberFunction>(&bar); } Now the compiler (visual studio 2010) gives me an error he cannot convert the const member function to a non-const function

Weird pointer to member function syntax

社会主义新天地 提交于 2019-12-05 13:55:49
I understand how to declare the type of a function: typedef void (typedef_void_f)(); // typedef_void_f is void() using alias_void_f = void(); // alias_void_f is void() And it can be used to declare function pointers: void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; } typedef_void_f *a = function; // pointer to void() alias_void_f *b = function; // pointer to void() For member function pointers the syntax is slightly more complicated: struct S { void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; } }; typedef void (S::*typedef_void_m_f)(); using alias_void_m_f = void (S::*)()

Can a function pointer with a const argument be used as a function pointer with a nonconst argument?

ぃ、小莉子 提交于 2019-12-05 13:47:18
问题 Perhaps the title isn't clear in itself... I have a function f (provided by some library) that takes as an argument a function pointer of signature void g(int*) , i.e. void f(void (*g)(int*)); However, I would like to use it using a function g (that I defined) with signature void g(const int*) . A priori, I can't see how this can violate any const-correctness, as all the signature of f says is that g will only ever be called with a (non- const ) int* (non- const ), and indeed I can call a