function-pointers

Get a pointer to the current function in C (gcc)?

霸气de小男生 提交于 2019-11-27 02:49:43
问题 is there a magic variable in gcc holding a pointer to the current function ? I would like to have a kind of table containing for each function pointer a set of information. I know there's a __func__ variable containing the name of the current function as a string but not as a function pointer. This is not to call the function then but just to be used as an index. EDIT Basically what i would like to do is being able to run nested functions just before the execution of the current function (and

C++ Using Class Method as a Function Pointer Type

家住魔仙堡 提交于 2019-11-27 02:07:06
In a C lib, there is a function waiting a function pointer such that: lasvm_kcache_t* lasvm_kcache_create(lasvm_kernel_t kernelfunc, void *closure) where lasvm_kernel_t is defined as: typedef double (*lasvm_kernel_t)(int i, int j, void* closure); Now, if I send a method defined in a class to lasvm_kcache_create: double cls_lasvm::kernel(int i, int j, void *kparam) ... lasvm_kcache_t *kcache=lasvm_kcache_create(&kernel, NULL); I get: "cannot convert ‘double (cls_lasvm:: )(int, int, void )’ to ‘double ( )(int, int, void )’" What should I do? I'm assuming that the closure argument is a context

Comparing std::functions for equality?

倖福魔咒の 提交于 2019-11-27 02:03:26
问题 How can I compare two C++11 std::function s with operator== , and return true if both of said function s refer to the same function pointer? 回答1: operator== for std::function compares a std::function with a null pointer, as far as I can tell the standard does not provide any details as to why. Although, this boost FAQ entry, Why can't I compare boost::function objects with operator== or operator!=? provides a rationale and as far as I can tell should be applicable to std::function as well.

C++ Call Pointer To Member Function

大憨熊 提交于 2019-11-27 01:49:16
I have a list of pointers to member functions but I am having a difficult time trying to call those functions... whats the proper syntax? typedef void (Box::*HitTest) (int x, int y, int w, int h); for (std::list<HitTest>::const_iterator i = hitTestList.begin(); i != hitTestList.end(); ++i) { HitTest h = *i; (*h)(xPos, yPos, width, height); } Also im trying to add member functions to it here std::list<HitTest> list; for (std::list<Box*>::const_iterator i = boxList.begin(); i != boxList.end(); ++i) { Box * box = *i; list.push_back(&box->HitTest); } Pointers to non-static members are a unique

Error with address of parenthesized member function

拟墨画扇 提交于 2019-11-27 01:32:55
I found something interesting. The error message says it all. What is the reason behind not allowing parentheses while taking the address of a non-static member function? I compiled it on gcc 4.3.4. #include <iostream> class myfoo{ public: int foo(int number){ return (number*10); } }; int main (int argc, char * const argv[]) { int (myfoo::*fPtr)(int) = NULL; fPtr = &(myfoo::foo); // main.cpp:14 return 0; } Error: main.cpp:14: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&myfoo::foo' From the

Function pointer arrays in Fortran

邮差的信 提交于 2019-11-27 01:11:07
I can create function pointers in Fortran 90, with code like real, external :: f and then use f as an argument to another function/subroutine. But what if I want an array of function pointers? In C I would just do double (*f[])(int); to create an array of functions returning double and taking an integer argument. I tried the most obvious, real, external, dimension(3) :: f but gfortran doesn't let me mix EXTERNAL and DIMENSION. Is there any way to do what I want? (The context for this is a program for solving a system of differential equations, so I could input the equations without having a

static vs extern “C”/“C++”

房东的猫 提交于 2019-11-27 00:58:20
What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just lucky or... class X { public: static void proxy(int i) {} } makecontext(..., (void (*)(void)) X::proxy, ...); vs extern "C" void proxy(int i) {} makecontext(..., (void (*)(void)) proxy, ...); EDIT: Can you show a compiler or architecture where the static member version does not work

Get a pointer to object's member function

余生长醉 提交于 2019-11-27 00:43:37
问题 Here is the problem: 1) I have a class like so: class some_class { public: some_type some_value; int some_function(double *a, double *b, int c, int d, void *e); }; 2) Inside some_function , I use some_values from some_class object to get a result. 3) So, I have a concrete object and I want to get a pointer to this object some_function . Is it possible? I can't use some_fcn_ptr because the result of this function depends on the concrete some_value of an object. How can I get a pointer to some

How to create a typedef for function pointers

蓝咒 提交于 2019-11-27 00:35:41
问题 I think it would be easier to use function pointers if I created a typedef for a function pointer, but I seem to be getting myself tripped up on some syntax or usage or something about typedef for function pointers, and I could use some help. I've got int foo(int i){ return i + 1;} typedef <???> g; int hvar; hvar = g(3) That's basically what I'm trying to accomplish I'm a rather new C programmer and this is throwing me too much. What replaces <???> ? 回答1: Your question isn't clear, but I

How to call the function using function pointer?

♀尐吖头ヾ 提交于 2019-11-27 00:35:41
问题 Suppose I have these three functions: bool A(); bool B(); bool C(); How do I call one of these functions conditionally using a function pointer, and how do I declare the function pointer? 回答1: You can do the following: Suppose you have your A,B & C function as the following: bool A() { ..... } bool B() { ..... } bool C() { ..... } Now at some other function, say at main: int main() { bool (*choice) (); // now if there is if-else statement for making "choice" to // point at a particular