function-pointers

C++: Function pointer to functions with variable number of arguments

泪湿孤枕 提交于 2019-11-27 22:51:22
I'm trying to figure out a way of how to be able to assign a function pointer to functions with different number of arguments. I have a while loop which takes a number of different functions as a conditional statement, so instead of writing multiple while loops with exactly the same code inside I'd like to have one with a function pointer. All the functions are of format bool f(...) . I think some code will best illustrate what I mean: int a, b, c, d; MyClass* my_class; typedef bool (MyClass::*my_fun_t)(); my_fun_t my_fun; if (condition1) my_fun = &MyClass::function_one(); else if (condition2)

Is there an elegant way to avoid dlsym when using dlopen in C?

百般思念 提交于 2019-11-27 22:30:26
I need to dynamically open a shared library lib.so if a specific condition is met at runtime. The library contains ~700 functions and I need to load all their symbols. A simple solution is to define the function pointers to all symbols contained in lib.so , load the library using dlopen and finally get the addresses of all symbols using dlsym . However, given the number of functions, the code implementing this solution is very cumbersome. I was wondering if a more elegant and concise solution exists, maybe with an appropriate use of macros for defining the function pointers. Thanks! You could

Do function pointers need an ampersand [duplicate]

蓝咒 提交于 2019-11-27 21:49:06
This question already has an answer here: Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'? 2 answers In C/C++, if I have a the following functions: void foo(); void bar(void (*funcPtr)()); Is there a difference between these two calls: bar(foo); bar(&foo); ? No, there is no difference, since function can be implicitly converted to pointer to function. Relevant quote from standard (N3376 4.3/1). An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function. Is there a difference between

Function Pointer in C

点点圈 提交于 2019-11-27 21:28:59
问题 How can I create a "function pointer" (and (for example) the function has parameters) in C? 回答1: http://www.newty.de/fpt/index.html typedef int (*MathFunc)(int, int); int Add (int a, int b) { printf ("Add %d %d\n", a, b); return a + b; } int Subtract (int a, int b) { printf ("Subtract %d %d\n", a, b); return a - b; } int Perform (int a, int b, MathFunc f) { return f (a, b); } int main() { printf ("(10 + 2) - 6 = %d\n", Perform (Perform(10, 2, Add), 6, Subtract)); return 0; } 回答2: typedef int

C++ understanding cocos2d-x use of function pointers

◇◆丶佛笑我妖孽 提交于 2019-11-27 21:26:04
问题 I am experimenting around with extending cocos2d-x CCMenuItem components and came across something I have not seen before in C++. It would be helpful if someone would elaborate on what is going on with their function pointer declarations The base class for most cocos2d-x objects is CCObject which has the following definition class CC_DLL CCObject : public CCCopying { public: // Code omitted }; // The part in which I have a question about typedef void (CCObject::*SEL_SCHEDULE)(float); typedef

C++ Conversion operator for converting to function pointer

不打扰是莪最后的温柔 提交于 2019-11-27 21:10:37
I'm been grinding my head against an idea that is simple enough in my head, but I can't figure out how to implement in C++. Normally, I can declare a class with a conversion operator like in this simple example: class Foo { private: int _i; public: Foo( int i ) : _i(i) { } operator int( ) const { return i; } }; So now I can write awesome stuff like int i = Foo(3); But in my particular case, I would like to provide an operator for converting an object to a function pointer (e.g. converting a Bar instance to a int(*)(int, int) function pointer). Here's what I initially tried: class Bar { private

Implement callback function in JNI using Interface

梦想的初衷 提交于 2019-11-27 20:11:42
I need to implement callback function in Java using “interface”. I have wrote the application part as MyJavaFunction(int size, m_GetSizeInterface); m_GetSizeInterface is an Interface which contains the callback function GetSize. This GetSize method is override in the application. In JNI I need to call a CPP function having prototype int MyCPPFunction(int size, int (*callback)(int* ID)); How can I pass this GetSize as parameter to MyCPPFunction in JNI? Please help public int GetSize (m_SizeClass arg0) { g_size = arg0.size; return 0; } Dave The complication here is that you want to invoke native

Why the size of a pointer to a function is different from the size of a pointer to a member function?

帅比萌擦擦* 提交于 2019-11-27 20:04:52
Isn't a pointer just an address? Or I'm missing something? I tested with several types of pointers: pointers to any variables is the same (8B on my platform) pointers to functions are the same size, as pointers to variables (8B again) pointers to functions with different parameters - still the same (8B) BUT pointers to member functions are bigger - 16B on my platform. Three things: Why are pointers to member functions bigger? What more information do they need? As far as I know, the standard says nothing about the size of a pointer, except that void* must be able to "contain" any pointer type.

Alternative to c++ static virtual methods

只谈情不闲聊 提交于 2019-11-27 19:17:57
In C++ is not possible to declare a static virtual function, neither cast a non-static function to a C style function pointer. Now, I have a plain ol' C SDK that uses function pointers heavily. I have to fill a structure with several function pointers. I was planning to use an abstract class with a bunch of static pure virtual methods, and redefine them in derived classes and fill the structure with them. It wasn't until then that I realized that static virtual are not allowed in C++. Also this C SDKs function signature doesn't have a userData param. Is there any good alternative? The best I

Passing member function pointer to member object in c++

梦想与她 提交于 2019-11-27 18:35:17
I have a problem with using a pointer to function in C++. Here is my example: #include <iostream> using namespace std; class bar { public: void (*funcP)(); }; class foo { public: bar myBar; void hello(){cout << "hello" << endl;}; }; void byebye() { cout << "bye" << endl; } int main() { foo testFoo; testFoo.myBar.funcP = &byebye; //OK testFoo.myBar.funcP = &testFoo.hello; //ERROR return 0; } Compilator returns an error at testFoo.myBar.funcP = &testFoo.hello; : ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say '&foo::hello' cannot convert