3 ms·
Ask HN: What is Rob Pike's function pointer advice in practice?
After reading Rob Pike's notes on function pointers
"I argue that clear use of function pointers is the heart of object-oriented programming.
Given a set of operations you want to perform on data, and a set of data types you want
to respond to those operations, the easiest way to put the program together is with a
group of function pointers for each type. This, in a nutshell, defines class and
method." From http://doc.cat-v.org/bell_labs/pikestyle
I wanted to understand what he meant.
Take an contact-card system. You have a struct contact_card { char* name, char* postcode, int telelphone } and an array of these making up your contacts. Then you have functions such as get_contacts_by_name(contact_card[], char* name) etc return an array of the contact_cards.
Using his advice, how would function pointers help here? All I can think of is a function such as contact_card_system((void*)get_contacts_by_name, contact_cards_array, "Rob Pike") that applies the arguments to the passed function pointer. And other functions such as get_contacts_by_postcode could be also passed in.
But I'm not sure that's really it. Any thoughts?