Why use function pointers?

What do function pointers need? The standard answer for this seems to callbacks, but why can't we just pass a function?

The book I read in C ++ demonstrates passing a function as a parameter and confirms that the compiled actually turns it into a pointer to a function and passes it instead, because functions are not actual objects. He showed the equivalent code using function pointers, which was a bit more complicated - if the code is equivalent, why use a function pointer.

I assume that there is a case where it is simply impossible to pass a function, and instead you need to pass a pointer? Can someone give me an example of this case, as this will help me understand why useful pointers are useful.

Consider the following code:

#include <iostream> using namespace std; int add(int x) { return ++x; //this is a copy, so it doesn't touch x } void runprint(int function(int x), int x) { cout << function(x) << endl; } int main() { runprint(add, 1); return 0; } 

We pass the function as a parameter, not a pointer. A function that takes a function (!) Does not accept a pointer.

+1
source share
2 answers

TL DR

"Function" and "function pointer" are the same.


There is the concept of a pointer and the syntax of its use; it’s not clear what you are asking for.

Concept

A pointer to a function may differ from the function itself (the difference is not useful in C ++ - see below) in that the function can take up a lot of space - its code can be arbitrarily complex. Manipulating (e.g. copying or searching / changing) function code is rarely useful, therefore c / C ++ does not support it at all. If you want to change the function code, hover over char* using all the necessary precautions (I never did).

So, if you write C, all you need is function pointers.

But...

Syntax

If you have a p pointer for a function, how do you want to call the function?

 (*p)(18); // call the function with parameter 18 p(18); // the same, but looks better! 

There is a slightly cleaner syntax that does not contain the * sign. To support it, the c / C ++ authors came up with the concept of "decay" - when your code mentions a "function", the compiler silently "fixes" it instead of a "pointer to a function" (in almost all cases; excuse me for not specifying Further). This is very similar to the "decay" of an array into a pointer mentioned by vsoftco.

So in your example

 void runprint(int function(int x), int x) { cout << function(x) << endl; } 

the type of function is actually the type of function pointer. Indeed, if you are trying to "overload":

 void runprint(int (*function)(int x), int x) { cout << function(x) << endl; } 

the compiler will complain about two identical functions with the same set of parameters.

Also, when creating a variable of type function / pointer-to-function

 runprint(add, 1); 

it also doesn't matter:

 runprint(&add, 1); // does exactly the same 

PS When declaring a function that receives a callback, I basically saw (and used) an explicitly written pointer. It only occurred to me now that he does not agree to rely on decomposition of the function-to-pointer when calling the callback, but not when declaring my code. So if the question

why does everyone declare callbacks using pointer-to-function syntax when the function syntax is sufficient?

I would answer "out of habit."

+1
source

function is considered a g ++ function pointer:

 $ cat fp.cpp #include <typeinfo> #include <iostream> using namespace std; int add(int x) { return ++x; //this is a copy, so it doesn't touch x } void runprint(int function(int x), int x) { cout << typeid(function).name() << endl; int (*f)(int); // function pointer compatible with function argument f = function; cout << typeid(f).name() << endl; cout << function(x) << endl; } int main() { runprint(add, 1); return 0; } $ g++ -Wall -std=c++11 -o fp fp.cpp $ ./fp PFiiE PFiiE 2 
+3
source

Source: https://habr.com/ru/post/1235690/


All Articles