Pointers to functions, when to use?

Discussion in 'C++' started by soni29, Sep 1, 2003.

  1. soni29

    soni29 Guest

    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.
     
    soni29, Sep 1, 2003
    #1
    1. Advertising

  2. 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
     
    Nils Petter Vaskinn, Sep 1, 2003
    #2
    1. Advertising

  3. "soni29" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 1, 2003
    #3
  4. soni29

    osmium Guest

    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.
     
    osmium, Sep 1, 2003
    #4
  5. soni29

    llewelly Guest

    "Nils Petter Vaskinn" <> 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()() :)
     
    llewelly, Sep 1, 2003
    #5
  6. 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
     
    Thomas Matthews, Sep 2, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S?ren Gammelmark
    Replies:
    1
    Views:
    1,985
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    883
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    326
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    732
  5. ajaybgr
    Replies:
    18
    Views:
    1,085
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page