Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the only use of function pointers to implement callbacks?

I have come across the fact that function pointers can be used to implement callbacks. Is there any other usage of function pointers? Is there any other situation that function pointers proved to be useful?

like image 837
Can't Tell Avatar asked Aug 30 '26 13:08

Can't Tell


1 Answers

How about sorting? Pass in a function pointer to compare any two elements.

How about filtering? Pass in a function pointer to decide whether an input element should be contained in the output of a filter.

How about transformations? Pass in a function pointer to convert an input element to an output element.

These are all collection-based uses, but they're very useful. (The broad equivalent of function pointers in .NET is delegates, and they're the basis of LINQ, which allows very simple querying, transformations, grouping etc.)

Anywhere you want to be able to abstract out the idea of "a single piece of behaviour", writing a generic function which doesn't need to know the details of that behaviour, a function pointer could be useful.

like image 151
Jon Skeet Avatar answered Sep 01 '26 05:09

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!