function-pointers

Converting class function pointer to void* or vice versa

末鹿安然 提交于 2020-01-06 08:12:47
问题 Im trying to compare the address of two functions for equality. Type of my stored function is known. This system normally works, consider the following code (written as sample not from the program): virtual bool compare(void *fn2) { void (*fn)(int); if(fn==fn2) return true; } However when class functions came into consideration the same method doesn't work. virtual bool compare(void *fn2) { void(__thiscall myclass::*fn)(int); void *fn2; if(fn==fn2) //error C2440 type cast: cannot convert void

Function pointer in Visual Studio 2012

那年仲夏 提交于 2020-01-06 08:08:42
问题 Please explain me where I am wrong. I want to switch between several encoding utilities using pointer to function. I declare it like int (*enc_routine)(); Later I switch coding utilities like enc_routine = g723_24_encoder; where utility by itself is something like extern int g723_24_encoder( int sample, int in_coding, struct g72x_state *state_ptr); Everything was cute and fine on Linux, but now I am on Visual Studio 2012 and it says: a value of type "int (*)(int sample, int in_coding, g72x

Python function pointer

跟風遠走 提交于 2020-01-06 06:01:12
问题 I have a function name stored in a variable like this: myvar = 'mypackage.mymodule.myfunction' and I now want to call myfunction like this myvar(parameter1, parameter2) What's the easiest way to achieve this? 回答1: funcdict = { 'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction, .... } funcdict[myvar](parameter1, parameter2) 回答2: It's much nicer to be able to just store the function itself, since they're first-class objects in python. import mypackage myfunc = mypackage.mymodule

scope of struct pointers in functions

不问归期 提交于 2020-01-05 19:12:21
问题 If i have the following in a function: struct example *A=malloc(sizeof(struct example)); does the node/memory space created where A points to get destroyed after the function ends/leaves? Or does it stay in the heap? 回答1: No. memory allocated by malloc will stay allocated until free 'd. Doing things like this: char * function(void) { char c = 'a'; return &c; } on the other hand, is bad, because this object DOES go out of scope when the function ends. malloc allocates memory on the heap.

scope of struct pointers in functions

梦想与她 提交于 2020-01-05 19:12:03
问题 If i have the following in a function: struct example *A=malloc(sizeof(struct example)); does the node/memory space created where A points to get destroyed after the function ends/leaves? Or does it stay in the heap? 回答1: No. memory allocated by malloc will stay allocated until free 'd. Doing things like this: char * function(void) { char c = 'a'; return &c; } on the other hand, is bad, because this object DOES go out of scope when the function ends. malloc allocates memory on the heap.

C++ Single function pointer for all template instances

狂风中的少年 提交于 2020-01-05 02:27:09
问题 Is there a concise way to point to all instances of a templated function without using macros? I have several templated functions that I want to test across a variety of types: template<typename T> void function1() { return; } template<typename T> void function2() { return; } template<typename T> void function3() { return; } I can do this with a macro: #define TEST_ACROSS_TYPES(fn) \ fn<int>(); \ fn<bool>(); \ fn<char>(); \ fn<double>(); \ TEST_ACROSS_TYPES(function1); TEST_ACROSS_TYPES

Passing member function to another object's member function C++

人走茶凉 提交于 2020-01-04 23:45:55
问题 I am having issues trying to pass a function as an argument in another object's function. I am well aware there are many similar topics but I either can't get their solution to work or can't understand them. class foo { public: void func1(void (*Drawing)(void)); template<class T> void func2(void (T::*Drawing)(void)); }; class bar { private: foo myFoo; void Drawing(); void func3() { // Attempt 1 myFoo.func1(Drawing); // Attempt 2 myFoo.func2<bar>(&bar::Drawing); } }; So in my first attempt, I

Casting a pointer to a method of a derived class to a pointer to a method of a base class

橙三吉。 提交于 2020-01-04 15:53:50
问题 Is it legal to cast a pointer to a method of derived class to a pointer to a method of base class , even though the base class does not declare any methods, especially of the "casted" method is called through an object of type base class, as follows: // works in VS 2008 and g++ 4.5.3 struct Base { }; struct Fuu : public Base { void bar(){ std::cout << "Fuu::bar" << std::endl; } void bax(){ std::cout << "Fuu::bax" << std::endl; } }; struct Foo : public Base { void bar(){ std::cout << "Foo::bar

Casting a pointer to a method of a derived class to a pointer to a method of a base class

偶尔善良 提交于 2020-01-04 15:53:07
问题 Is it legal to cast a pointer to a method of derived class to a pointer to a method of base class , even though the base class does not declare any methods, especially of the "casted" method is called through an object of type base class, as follows: // works in VS 2008 and g++ 4.5.3 struct Base { }; struct Fuu : public Base { void bar(){ std::cout << "Fuu::bar" << std::endl; } void bax(){ std::cout << "Fuu::bax" << std::endl; } }; struct Foo : public Base { void bar(){ std::cout << "Foo::bar

Pointer to a function with reduced arguments

烈酒焚心 提交于 2020-01-04 06:43:46
问题 I've a problem with function pointers: I need to numerically integrate a function and want therefore pass a pointer with the function to the "integrator". The problem is, that the function to be integrated takes more then just one argument. Something like: double f(int i, double x){ // i to switch the function, x to evaluate if(i==1) {return sin(x);} if(i==2) {return exp(x);} } double integrate(double (*function)(double), double x0, double x1){ //integrate the passed *function from x0 to x1 }