Pointers to functions, when to use?

S

soni29

hi,
i currently reading a book on C++, the author just covered the topic
of pointer to functions, but never mentioned the use. if possible
could someone please tell me when to use them, why not such make the
function call without the pointer?

Thank you.
 
N

Nils Petter Vaskinn

hi,
i currently reading a book on C++, the author just covered the topic
of pointer to functions, but never mentioned the use. if possible
could someone please tell me when to use them, why not such make the
function call without the pointer?

Thank you.

One example (from C, but you can use it in C++)

#include <stdlib.h>

void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

Basically qsort takes as paarmeter an array of objects of some unknown
type, the size of those objects and the number of those elements in the
array. Then for the final argument: A function that knows how to compare
two elements of the array (returning a value less than zero, zero or
greater than zero depending on how the two parameters compare)

This way the qsort function doesn't have to know anything about what the
elements are. And when you wish to sort an array of something you only
have to provide a compare function instead of a complete qsort
implementation.

This is often more useful in C than in C++, because in C++ an
implementatin of qsort could define an interface (Sortable or something
like that) that the elements has to implement.

But suppose the elements you wish to sort may be out of your control (from
a library or somesuch) you can either subclass just to add the sortable
interface, or you can provide a sorting function.


regards
NPV
 
J

John Harrison

soni29 said:
hi,
i currently reading a book on C++, the author just covered the topic
of pointer to functions, but never mentioned the use. if possible
could someone please tell me when to use them, why not such make the
function call without the pointer?

Thank you.

That's a bit like asking 'why use a variable when you know the value is 42'.

Sometimes you want to use a function pointer variable or parameter, and have
that variable or parameter point at different functions at different times.
That way you can call the function wherever it happens to be pointing.

For example (stupid example)

void print_hello()
{
cout << "hello\n";
}

void print_goodbye()
{
cout << "goodbye\n";
}

void call_twice(void (*func)())
{
func();
func();
}

int main()
{
call_twice(print_hello);
call_twice(print_goodbye);
}

john
 
O

osmium

soni29 said:
i currently reading a book on C++, the author just covered the topic
of pointer to functions, but never mentioned the use. if possible
could someone please tell me when to use them, why not such make the
function call without the pointer?

Consider writing a function to evaluate a definite integral and come up with
a numerical answer. The techniques used all require a function that, given
x, will evaluate f(x). The calling program provides an appropriate f(x)
function and passes a pointer to that function to the function that does the
actual evaluation. The integral solver is generalized, the calling program
allows a particular solution.
 
L

llewelly

Nils Petter Vaskinn said:
One example (from C, but you can use it in C++)

#include <stdlib.h>

void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));

Basically qsort takes as paarmeter an array of objects of some unknown
type, the size of those objects and the number of those elements in the
array. Then for the final argument: A function that knows how to compare
two elements of the array (returning a value less than zero, zero or
greater than zero depending on how the two parameters compare)

This way the qsort function doesn't have to know anything about what the
elements are. And when you wish to sort an array of something you only
have to provide a compare function instead of a complete qsort
implementation.

This is often more useful in C than in C++, because in C++ an
implementatin of qsort could define an interface (Sortable or something
like that) that the elements has to implement.
[snip]

But that's not what the standard C++ library does. e.g.

#include<algorithm>
#include<cstdlib>
#include<vector>

bool gt(int lhs, int rhs){return rhs < lhs;}

int main()
{
std::vector<int> v(100);
std::generate(v.begin(), v.end(), &std::rand);
std::sort(v.begin(), v.end(), &gt);
}

Note both generate() and sort() take function pointers as parameters
(as well as anything else that supports operator()() :)
 
T

Thomas Matthews

soni29 said:
hi,
i currently reading a book on C++, the author just covered the topic
of pointer to functions, but never mentioned the use. if possible
could someone please tell me when to use them, why not such make the
function call without the pointer?

Thank you.

I use function pointers when parsing instructions or messages.
I can create a table of <key, function pointer> records. I scan
the key field of each record. If the key matches, I execute the
function by dereferencing the pointer. Search the web and this
newsgroup for "Factory Design Pattern".

This technique also works well with menus. You use a table of
<selection number, text, function_pointer>. Write one driver to
parse the table and print the menu. To add a new selection,
just add a new record to the table (along with a function to
process the given selection).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top