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
40
votes
12 answers

Alternative to c++ static virtual methods

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…
raven
  • 2,574
  • 2
  • 27
  • 49
39
votes
3 answers

How do I compare two functions for pointer equality in the latest Go weekly?

In Go, is there any way to compare two non-nil function pointers to test for equality? My standard of equality is pointer equality. If not, is there any particular reason why pointer equality is not allowed? As of now, if I attempt to do this in the…
BurntSushi5
  • 13,917
  • 7
  • 52
  • 45
39
votes
3 answers

Please explain syntax rules and scope for "typedef"

What are the rules? OTOH the simple case seems to imply the new type is the last thing on a line. Like here Uchar is the new type: typedef unsigned char Uchar; But a function pointer is completely different. Here the new type is pFunc: typedef…
ValenceElectron
  • 2,678
  • 6
  • 26
  • 27
39
votes
4 answers

C++ Call Pointer To Member Function

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::const_iterator i =…
user1670407
39
votes
7 answers

C syntax for functions returning function pointers

Consider the following typedefs : typedef int (*f1)(float); typedef f1 (*f2)(double); typedef f2 (*f3)(int); f2 is a function that returns a function pointer. The same with f3, but the type of the function, the pointer to which f3 returns, is f2.…
keveman
  • 8,427
  • 1
  • 38
  • 46
38
votes
2 answers

generic member function pointer as a template parameter

Consider this code: #include using namespace std; class hello{ public: void f(){ cout<<"f"<
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
38
votes
11 answers

What is the address of a function in a C++ program?

As the function is set of instruction stored in one contiguous block of memory. And address of a function (entry point) is the address of the first instruction in the function. (from my knowledge) And thus we can say that the address of function and…
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
37
votes
4 answers

What's the use of multiple asterisks in the function call?

I can't think of any practical use of multiple asterisks in the function call: void foo(int a, char b) { } int main(void) { (**************foo)(45, 'c'); //or with pointer to function: void (*ptr)(int, char) = foo; (******ptr)(32,…
Quentin
  • 1,090
  • 13
  • 24
35
votes
5 answers

Get a pointer to object's member function

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…
Alex Hoppus
  • 3,821
  • 4
  • 28
  • 47
35
votes
7 answers

Can a static function be called through a function pointer in C?

I believe that a static function in a source file cannot be called directly from outside the file. However, if I somehow manage to get a pointer to this function into another file, can I then call this function from that file. If yes, is there any…
AnkurVj
  • 7,958
  • 10
  • 43
  • 55
35
votes
4 answers

Why does this function call behave sensibly after calling it through a typecasted function pointer?

I have the following code. There is a function that takes two int32. Then I take a pointer to it and cast it to a function that takes three int8 and call it. I expected a runtime error but program works fine. Why this even…
Divano
  • 583
  • 5
  • 12
35
votes
2 answers

std::function to member function

#include struct A { int func(int x, int y) { return x+y; } }; int main() { typedef std::function Funcp; A a; //Funcp func = std:::bind(&A::func, &a); Funcp func =…
hidayat
  • 9,493
  • 13
  • 51
  • 66
35
votes
2 answers

Function pointers with default parameters in C++

How does C++ handle function pointers in relation to functions with defaulted parameters? If I have: void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float =…
35
votes
4 answers

Cast to function pointer

I have come across the line of code shown below. I think it may be a cast to a function pointer that returns void and takes a void pointer. Is that correct? (void (*)(void *))SGENT_1_calc
Toby
  • 9,696
  • 16
  • 68
  • 132
34
votes
2 answers

Error with address of parenthesized member function

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 class myfoo{ public: …
Mahesh
  • 34,573
  • 20
  • 89
  • 115