Thread: What does this declaration mean void PrintVals(void (*)(int&, int&),int&, int&);

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    13

    What does this declaration mean void PrintVals(void (*)(int&, int&),int&, int&);

    The title pretty much says it all, what does this declaration mean:
    Code:
    void PrintVals(void (*)(int&, int&),int&, int&);
    I've been challenged by a book online (recommended from this website, it's definitely the best starter tutorial I've come across, though it's much harder work than 21 days).

    I'm currently on day 14, on the subject of pointers to functions and in this case pointers to functions to other functions. Teach Yourself C++ in 21 Days.

    Go find a C++ programmer and ask him what this declaration means:

    void PrintVals(void (*)(int&, int&),int&, int&);

    This is the kind of declaration that you use infrequently and probably look up in the book each time you need it, but it will save your program on those rare occasions when it is exactly the required construct.
    I understand that the function (PrintVals) takes 3 arguments and returns void. 2 of these arguments are references to integers and the third is a pointer to a function that takes 2 references to integers and also returns void. However, it doesn't state which function it points to.

    As a complete stab in the dark, I would guess it was pointing to itself, and the asterix represents a "this" pointer, in a kind of recursive like function where it calls itself? The chapter says I will rarely use it and I will probably have to look it up every time I use it, but it'll be nice to get a simple understanding early on instead of when I'm under pressure to use it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to pass something in to the function, of course. Just like you have to pass in two ints.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    So it's a prototype of a function? How would I intput which function to use? Would it be as simple as:
    Code:
    void PrintVals(void (*someFunc)(5, 5), 5, 5);

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to pass the function in as an argument. If you ever use the word "void" when you are trying to call a function, then You Are Doing It Wrong.
    Code:
    void somefunction(int& x, int& y) {
       x++;
       y++;
    }
    
    .
    .
    .
    
    PrintVals(somefunction, 2, 4);

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    OK thank you, it's simpler than I thought! And yeah, silly mistake with the voids, it's too late for me to think right now.

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    Code:
    void PrintVals(void (*)(int&, int&),int&, int&);
    void foo(int& x, int& y) {
       x++;
       y++;
    }
    .
    .
    .
    
    PrintVals(foo(2,3), 4, 5); //or PrintVals(*foo(2,3), 4, 5);

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that valthyx's example is wrong because the first argument should be a function pointer, and because the other parameters are int references.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  2. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  3. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  4. What is this mean? static void *name (void *data)
    By beyonddc in forum C++ Programming
    Replies: 14
    Last Post: 05-05-2004, 03:06 PM
  5. int memCopy(void* , void*, int size)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:02 PM