What is the importance of function pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the importance of function pointer

Would like to know in which case function pointer is must to implement... would like to know scenario when it is important to implement...

7th Mar 2019, 4:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 4
The point of my example code is to allow the programmer to modify the behaviour of the class without touching the code of the class itself. You couldn't call the sort methods directly because the vector is private in it. You can possible specify very different functions that use different sorting algorithms, depending on the programmers needs. Even plug in extra functionality in the functions, like logging without ever needing to read the class' definition. Another example that came to mind is that function pointers are pretty much a must when using asynchronous functions. These asynchronous functions have to do something when their task is finished and since your code does not wait for them, what should they do when they finish? You assign them a function for them to callback (<- they are called that) that handles the rest. Since you, as a programmer, have no way of knowing ahead of time what such callbacks should do you have to use function pointers so that you can specify a function later. You literally see them all over the place in javascript.
8th Mar 2019, 4:22 PM
Dennis
Dennis - avatar
+ 3
yes, I am able to relate your point of call back function.... I have used one such function in CAD domain... thank you for your fantastic explanation.
8th Mar 2019, 4:38 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
1 thing that immediatly comes to mind is that function pointers allow your classes and functions to be much more ... customizable. The stl library uses it in alot of its functions, but uses templates to allow any type of function. Some quick examples are https://en.cppreference.com/w/cpp/algorithm/transform https://en.cppreference.com/w/cpp/algorithm/sort The templates named: UnaryOperation, a function taking 1 argument; BinaryOperation, a function taking 2 arguments; Compare, a function taking 2 arguments and does a comparison between the 2; are all function pointers. As for classes, well, here is an example: https://code.sololearn.com/cH67qrFMBkiq/?ref=app In the example you can see that changing between 2 types of sorting methods is as easy as calling 1 simple function and does not require you to change anything else.
7th Mar 2019, 5:45 PM
Dennis
Dennis - avatar
+ 1
Dennis appreciate your response... I got an idea about it a bit more... another question comes to my mind now... we could have called required sort function directly as anyway we are passing which function to use at compile time only into another function..... what benifit is there to use function pointer... is there something which can't be achieved without function pointer...? or is this just the convention to use function pointer....
8th Mar 2019, 3:48 PM
Ketan Lalcheta
Ketan Lalcheta - avatar