About function pointers...

This is a discussion on About function pointers... within the C++ Programming forums, part of the General Programming Boards category; I looked through the tutorials on this site and I'm still unclear how exactly they work... In the example it ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    About function pointers...

    I looked through the tutorials on this site and I'm still unclear how exactly they work...

    In the example it shows as a prototype:

    Code:
    bool (*)(int, int)
    Does the name of the pointer itself go inside the first set of parenthases, with the *?

    That being asked, in what situations would you want to use function pointers? In the example, it looks to me like it is just as practical to use the return value of the function instead of the pointer, as either way the function needs to be evaluated...

    Thanks for your assistance, as always.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The name of your function pointer would be right of the asterisk. Function pointers are most useful in situations where you must call an unknown function defined by the user (of your code). A common example is in Windows programming. In order for your program to process messages sent by the operating system, you can give it the name of a callback function you have defined. You wouldn't have any reason to use function pointers in most applications and when programming in C++ (and you're developing for a framework that allows it) you can use functors instead.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    double add(double x, double y) {return x+y;}  // two functions to point to
    double subtract(double x, double y) {return x-y;}
    
    // ...
    
    double (*funcpointer)(double, double);  // declare the function pointer
        // it's a pointer to a function that returns a double and takes two doubles as arguments
    
    // ...
    
    if(input == "+") funcpointer = add;  // set the function pointer (method #1)
    else funcpointer = &subtract;  // method #2
    
    // ...
    
    funcpointer(num1, num2);  // call the function (method #1)
    (*funcpointer)(num1, num2);  // method #2
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    funcpointer(num1, num2);  // call the function (method #1)
    (*funcpointer)(num1, num2);  // method #2
    Neither of those statements would be useful.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Having a bad day 7stud?
    Woop?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That calls the function that the function pointer points to, which youŽll have to do eventually for it to be useful.

    Unless you mean the return value. Okay, then, do this:
    Code:
    double one = funcpointer(num1, num2);  // call the function (method #1)
    double two = (*funcpointer)(num1, num2);  // method #2
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21