Questions tagged [function-pointers]

A function pointer is a pointer to a function, which can be stored in a variable. It allows a run-time choice of which function to run.

In programming languages like C, function pointers can be used to simplify code by providing a simple way to select a function to execute based on run-time values.

Many "pure" object-oriented languages do not support function pointers. Something similar can be implemented in these kinds of languages, though, using references to interfaces that define a single member function. Microsoft .NET languages such as C# and Visual Basic .NET implement type-safe function pointers with delegates.

In other languages that support first-class functions, functions are regarded as data, and can be passed, returned, and created dynamically directly by other functions, eliminating the need for function pointers.

(Source: Wikipedia)

Popular Questions

4363 questions
53
votes
17 answers

How can I call a function using a function pointer?

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?
Priya
53
votes
8 answers

Function pointers casting in C++

I have a void pointer returned by dlsym(), I want to call the function pointed by the void pointer. So I do a type conversion by casting: void *gptr = dlsym(some symbol..) ; typedef void (*fptr)(); fptr my_fptr = static_cast(gptr) ; I have…
sud03r
  • 19,109
  • 16
  • 77
  • 96
51
votes
6 answers

Using a STL map of function pointers

I developed a scripting engine that has many built-in functions, so to call any function, my code just went into an if .. else if .. else if wall checking the name but I would like to develop a more efficient solution. Should I use a hashmap with…
Jack
  • 131,802
  • 30
  • 241
  • 343
51
votes
5 answers

Function pointer as an argument

Is it possible to pass a function pointer as an argument to a function in C? If so, how would I declare and define a function which takes a function pointer as an argument?
inquisitive
  • 1,558
  • 4
  • 16
  • 22
49
votes
3 answers

Do function pointers need an ampersand

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); ?
Baruch
  • 20,590
  • 28
  • 126
  • 201
48
votes
4 answers

convert std::bind to function pointer

I have a third-party library which has a method that takes a function pointer as the first parameter: int third_party_method(void (*func)(double*, double*, int, int, double*), ...); I want to pass a pointer to a class' method that is declared as…
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
46
votes
1 answer

How do I get the argument types of a function pointer in a variadic template class?

This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template class Foo { std::function m_f; public: …
steffen
  • 8,572
  • 11
  • 52
  • 90
46
votes
3 answers

Why do we use std::function in C++ rather than the original C function pointer?

What is the advantage of std::function over the original T1 (*)(T2)?
Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
45
votes
1 answer

How to declare an __stdcall function pointer

I tried this typedef void (* __stdcall MessageHandler)(const Task*); This compiles but gives me this warning (VS2003): warning C4229: anachronism used : modifiers on data are ignored I want to declare a pointer to a function with stdcall…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
43
votes
8 answers

How to pass a function pointer that points to constructor?

I'm working on implementing a reflection mechanism in C++. All objects within my code are a subclass of Object(my own generic type) that contain a static member datum of type Class. class Class{ public: Class(const std::string &n, Object…
Kareem
  • 1,026
  • 3
  • 10
  • 15
42
votes
3 answers

Passing any function as template parameter

I want to pass a function value as a template parameter to a function. Currently the best I managed to do is : template< typename F, F f > void pass() { ... } ...which is used: pass< decltype(&func), &func >(); What I would really like is to…
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
41
votes
7 answers

How to implement a "private/restricted" function in C?

I was asked a very interesting question during a C interview: How can you implement a function f() in such a way that it can only be called from a particular g() function. If a function other than g() tries to call f() it would result in a compiler…
luizleroy
  • 508
  • 1
  • 4
  • 6
41
votes
3 answers

How to create a typedef for function pointers

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…
SetSlapShot
  • 1,298
  • 1
  • 21
  • 48
40
votes
6 answers

C++ function pointer (class member) to non-static member function

class Foo { public: Foo() { do_something = &Foo::func_x; } int (Foo::*do_something)(int); // function pointer to class member function void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; } private: int…
Girish
  • 925
  • 3
  • 10
  • 20
40
votes
4 answers

What is guaranteed about the size of a function pointer?

In C, I need to know the size of a struct, which has function pointers in it. Can I be guaranteed that on all platforms and architectures: the size of a void* is the same size as a function pointer? the size of the function pointer does not differ…
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152