What is the point of function pointers?

2020-01-22 13:09发布

I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer.

Could you give some example of good use of function pointers (in C or C++)?

16条回答
唯我独甜
2楼-- · 2020-01-22 13:36

For OO languages, to perform polymorphic calls behind the scenes (this is also valid for C up to some point I guess).

Moreover, they're very useful to inject different behaviour to another function (foo) at runtime. That makes function foo higher-order function. Besides it's flexibility, that makes the foo code more readable since it let's you pull that extra logic of "if-else" out of it.

It enables many other useful things in Python like generators, closures etc.

查看更多
可以哭但决不认输i
3楼-- · 2020-01-22 13:41

Like Rich said above, it is very usual for functions pointers in Windows to reference some address that stores function.

When you programming in C language on Windows platform you basically load some DLL file in primary memory(using LoadLibrary) and to use the functions stored in DLL you need to create functions pointers and point to these address (using GetProcAddress).

References:

查看更多
叼着烟拽天下
4楼-- · 2020-01-22 13:45

A different perspective, in addition to other good answers here:

In C, there are only function pointers, there aren't any functions.

I mean, you write functions, but you cant manipulate functions. There's no run-time representation of a function as such. You can't even call "a function". When you write:

my_function(my_arg);

what you're actually saying is "perform a call to the my_function pointer with the specified argument". You're making a call via a function pointer. This decay to function pointer means that the following commands are equivalent to the previous function call:

(&my_function)(my_arg);
(*my_function)(my_arg);
(**my_function)(my_arg);
(&**my_function)(my_arg);
(***my_function)(my_arg);

and so on (thanks @LuuVinhPhuc).

So, you're already using function pointers as values. Obviously you would want to have variables for those values - and here is where all the uses other metion come in: Polymorphism/customization (like in qsort), callbacks, jump tables etc.

In C++ things are a bit more complicated, since we have lambdas, and objects with operator(), and even an std::function class, but the principle is still mostly the same.

查看更多
对你真心纯属浪费
5楼-- · 2020-01-22 13:46

Agree with all of the above, plus.... When you load a dll dynamically at runtime you'll need function pointers to call the functions.

查看更多
Evening l夕情丶
6楼-- · 2020-01-22 13:46

I am going to go against the current here.

In C, function pointers are the only way to implement customization, because there is no OO.

In C++, you can use either function pointers or functors (function objects) for the same result.

The functors have a number of advantages over raw function pointers, due to their object nature, notably:

  • They may present several overloads of the operator()
  • They can have state / reference to existing variables
  • They can be built on the spot (lambda and bind)

I personally prefer functors to function pointers (despite the boilerplate code), mostly because the syntax for function pointers can easily get hairy (from the Function Pointer Tutorial):

typedef float(*pt2Func)(float, float);
  // defines a symbol pt2Func, pointer to a (float, float) -> float function

typedef int (TMyClass::*pt2Member)(float, char, char);
  // defines a symbol pt2Member, pointer to a (float, char, char) -> int function
  // belonging to the class TMyClass

The only time I have ever seen function pointers used where functors could not was in Boost.Spirit. They have utterly abused the syntax to pass an arbitrary number of parameters as a single template parameter.

 typedef SpecialClass<float(float,float)> class_type;

But since variadic templates and lambdas are around the corner, I am not sure we will use function pointers in pure C++ code for long now.

查看更多
聊天终结者
7楼-- · 2020-01-22 13:46

My main use of them has been CALLBACKS: when you need to save information about a function to call later.

Say you're writing Bomberman. 5 seconds after the person drops the bomb, it should explode (call the explode() function).

Now there's 2 ways to do it. One way is by "probing" all bombs on the screen to see if they're ready to explode in the main loop.

foreach bomb in game 
   if bomb.boomtime()
       bomb.explode()

Another way is to attach a callback to your clock system. When a bomb is planted, you add a callback to make it call bomb.explode() when the time is right.

// user placed a bomb
Bomb* bomb = new Bomb()
make callback( function=bomb.explode, time=5 seconds ) ;

// IN the main loop:
foreach callback in callbacks
    if callback.timeToRun
         callback.function()

Here callback.function() can be any function, because it is a function pointer.

查看更多
登录 后发表回答