function-pointers

Pointer to current function

Is there any way to get a pointer to the current function, maybe through gcc extensions or some other trickery? Edit I'm curious whether it is possible to get the function pointer without ever explicitly using the function's name. I thought I had a good reason for wanting this, realized that I didn't really, but am still curious if it ...

Function Pointer

How is that function pointer better than if-else or switch case? Is it because function pointer helps callback functions and thus promotes asynchronous implementation? ...

C++ function-pointer and inheritance

I have an generic math-method, that operates under a set of functions (with a lot of variables and states, so it can't be static). I've implemented method in parent class and I want to declare a different set of functions in every child-class. I've try to do something like this: class A { public: typedef int (A::*func)(); func *...

Can function pointers be used to run "data"?

This is not something most people would probably use, but it just came to mind and was bugging me. Is it possible to have some machine code in say, a c-string, and then cast its address to a function pointer and then use it to run that machine code? ...

pointers in objective-c, how do i call the function in the right way - beginners question

i have a dynamically made prototype: typedef double ICEDouble; -(BOOL) getPosition:(SyDRpcInterfacePositionType)type longitude:(ICEDouble *)longitude latitude:(ICEDouble *)latitude; and i would call it so, because i have no plan, how to do it in the right way: NSNumber* longitudeReturn; NSNumber** latitudeReturn; [prx getPositi...

What's the point of using boost::mem_fn if we have boost::bind ?

I'm having a look at the Boost libraries that were included in C++'s Technical Report 1 and trying to understand what each does. I've just finished running an example for boost::mem_fn and now I'm wondering what's the point of using it instead of the better boost::bind. As far as I understand, both of them return a function object poin...

How to pass a pointer to a member function to a C function?

Possible Duplicate: Using a C++ class member function as a C callback function I'm writing an object-oriented library using a C library (winpcap). I need to pass the callback function that is called when a network packet arrives as a function pointer. I would like to pass a member function pointer to winpcap, to keep my design...

How to create map<string, class::method> in c++ and be able to search for function and call it?

I'm trying to create a map of string and method in C++, but I don't know how to do it. I would like to do something like that (pseudocode): map<string, method> mapping = { "sin", Math::sinFunc, "cos", Math::cosFunc, ... }; ... string &function; handler = mapping.find(function); int result; if (handler != NULL) result = (int) ...

Creating a list similar to .ctors from multiple object files

I'm currently at a point where I need to link in several modules (basically ELF object files) to my main executable due to a limitation of our target (background: kernel, targeting the ARM architecture). On other targets (x86 specifically) these object files would be loaded at runtime and a specific function in them would be called. At s...

Inserting <string, function pointer> into map

I'm having trouble with inserting some value_pairs into a map. Here's the basic idea. // private typedef Foo* (*Bar)( const std::string &x, int y ); typedef std::map<std::string, Bar> myMap; template<class T> Foo* DoThing( const std::string &x, int y ) { return new T( x, y ); } myMap m_map; // some map insertion code m_map.insert( ...

C, function pointer with arguments preset

Is something like this possible in C? #include <stdio.h> void print_str(char *str) { printf(str); } int main() { void (*f_ptr)() = print_str,"hello world"; f_ptr(); } //see "hello world" on stdout In short, I'd like to have a function pointer that "stores" the arguments. The point is that the function po...

Call a member function with bare function pointer

What's the best way to call a member function if you have an object and a bare function pointer pointing to the member? Essentially I want to call the function pointer with thiscall calling convention. Background: I'm looking up symbols in a shared library dynamically, obtaining a factory function pointer and a pointer to a certain memb...

How to define a function that can return a pointer to itself?

I want to write code like this: /*something*/ Fn() { ... } int main() { /*something*/ fn = Fn; while(fn) fn = fn(); return 0; } Is it possible to do this is a fully type safe way? Assume C, C++, D, C#, Java, or any other statically typed language. ...

Should lambda decay to function pointer in templated code?

I read somewhere that a lambda function should decay to function pointer if the capture list is empty. The only reference I can find now is n3052. With g++ (4.5 & 4.6) it works as expected, unless the lambda is declared within template code. For example the following code compiles: void foo() { void (*f)(void) = []{}; } But it do...

c++ Function pointer declaration causes program to crash on exit.

I declare the following two function pointers in my class: void (*ptrFunc)(void *); bool (*ptrValid)(char *); Now for some reason, the second pointer (ptrValid) causes the program to crash on exit. When I comment out the declaration the program exits fine, but when I un-comment it, it crashes. Nothing is being assigned to it, it isn...

cast pointer to functor, and call it

can I do something like: typedef void (*functor)(void* param); //machine code of function char functionBody[] = { 0xff,0x43,0xBC,0xC0,0xDE,.... } //cast pointer to function functor myFunc = (functor)functionBody; //call to functor myFunc(param); ...

Is it safe to call casted function pointers?

In particular, will the following ever not work as expected: typedef void(*func_p)(void*); void foo(int* data) { printf("%i\n",*data); } int main(int argc, char** argv) { func_p bar; int x = 42; bar = foo; bar((void*)&x); return 0; } ie, can I rely on data pointers (void*, int*, struct baz*,...

Function pointers working as closures in C++

Hi, Is there a way in C++ to effectively create a closure which will be a function pointer? I am using the Gnu Scientific Library and I have to create a gsl_function. This function needs to effectively "close" a couple of parameters available when I create it. Is there a nice trick to create a closure so that I don't have to pass all of...

Create a function pointer which takes a function pointer as an argument.

How to create a function pointer which takes a function pointer as an argument (c++)??? i have this code #include <iostream> using namespace std; int kvadrat (int a) { return a*a; } int kub (int a) { return a*a*a; } void centralna (int a, int (*pokfunk) (int)) { int rezultat=(*pokfunk) (a); cout<<rezultat<<endl; } ...

boost::function and plain function pointers: ambigous overload

Given the following member function overload to take various functors class Foo { public: void bar(boost::function<void(int)> func); void bar(boost::function<void(float)> func); void bar(boost::function<void(const std::vector<float>&)> func); } and the function void baz(float f) { std::cout << "float :" << f << st...