Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointers to functions, when to use?

Reply
Thread Tools

Pointers to functions, when to use?

 
 
soni29
Guest
Posts: n/a
 
      09-01-2003
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.
 
Reply With Quote
 
 
 
 
Nils Petter Vaskinn
Guest
Posts: n/a
 
      09-01-2003
On Mon, 01 Sep 2003 06:34:42 -0700, soni29 wrote:

> 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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-01-2003

"soni29" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      09-01-2003
soni29 writes:

> 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.


 
Reply With Quote
 
llewelly
Guest
Posts: n/a
 
      09-01-2003
"Nils Petter Vaskinn" <(E-Mail Removed)> writes:

> On Mon, 01 Sep 2003 06:34:42 -0700, soni29 wrote:
>
>> 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.

[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()()

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      09-02-2003
soni29 wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Does deleting a container of pointers also delete the (contained) pointers? Xamalek C++ 7 11-04-2003 04:17 PM
c++: pointers to pointers A C++ 3 10-29-2003 01:15 PM
pointers to pointers // exception handling error muser C++ 3 09-18-2003 06:19 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments