445,797 Members | 1,794 Online
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

# Function returning a function pointer?

 P: n/a How would you write a function returning a function pointer and why would you need to do this? Is it: int(*)(int&) fn(int& arg); Thanks!!! Dec 11 '05 #1
14 Replies

 P: n/a Protoman wrote: How would you write a function returning a function pointer and why would you need to do this? There are many reason why one function would return a function pointer. The most obvious are: - to give user a channel to access futher information - to give follow up which can be chain-invoked Is it: int(*)(int&) fn(int& arg); Thanks!!! The correct declaration is: int (*fn(int& arg))(int&); If this seems cryptic, read it outside in: -the return type is a pointer to int(int&) therefore: int (* [...])(int&); -the [...] is the function fn itself therefore: int (* fn(int& arg) )(int& arg); Alternatively, you can typedef the return type in advance: typedef int(*fn_ptr)(int&); fn_ptr fn(int& arg); Regards, Ben Dec 11 '05 #2

 P: n/a "Protoman" wrote in message news:11**********************@o13g2000cwo.googlegr oups.com How would you write a function returning a function pointer and why would you need to do this? Is it: int(*)(int&) fn(int& arg); Thanks!!! Given a function int foo(int &x) { ++x; return x; } a function that takes an int reference parameter arg and will return a pointer to foo takes the form int (*fn(int& arg))(int &) { return &foo; // the & is optional } However, you will give yourself less brain strain if you do it this way: typedef int (*fnptr)(int&); fnptr fn(int& arg) { return &foo; } As for why you would need to do this, perhaps fn would select the appropriate function pointer the program needs to use, based on the calculations it does with arg. -- John Carson Dec 11 '05 #3

 P: n/a Can I use such a construct to do currying? Dec 11 '05 #4

 P: n/a * Protoman: How would you write a function returning a function pointer and why would you need to do this? Is it: int(*)(int&) fn(int& arg); Thanks!!! See the FAQ about H O M E W O R K. -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Dec 11 '05 #5

 P: n/a THIS IS NOT HOMEWORK!!!!!!! I AM A COLLEGE MED STUDENT!!!! THIS IS A BLOODY HOBBY OF MINE!!!! NOTHING I EVER POST IS HOMEWORK!!!!! Dec 11 '05 #6

 P: n/a Protoman wrote: Can I use such a construct to do currying? You can simulate currying using function pointers Eg f x y = x is a curried function. It can be seen as a function which takes an object of type T and returns a function (poiner) which takes an object of type U and returns an object of type T. In pseudo code, it becomes T (*f (T))(U) HTH Dec 11 '05 #7

 P: n/a Neelesh Bodas wrote: Protoman wrote: Can I use such a construct to do currying? You can simulate currying using function pointers Eg f x y = x is a curried function. It can be seen as a function which takes an object of type T and returns a function (poiner) which takes an object of type U and returns an object of type T. In pseudo code, it becomes T (*f (T))(U) HTH I can't follow that; could you put that into C++ code and show me an example? Thanks!!! Dec 11 '05 #8

 P: n/a > See the FAQ about H O M E W O R K. I believe C++ declaration syntax is more than what people would struggle with as homework. Ben Dec 11 '05 #9

 P: n/a * benben: See the FAQ about H O M E W O R K. I believe C++ declaration syntax is more than what people would struggle with as homework. "and why would you need it" -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Dec 11 '05 #10

 P: n/a Neelesh Bodas wrote: Protoman wrote:Can I use such a construct to do currying? You can simulate currying using function pointers I believe function objects would be more appropriate as they can hold a state (the extra argument) As for the OP: std::bind1st, std::bind2nd, mem_fun and mem_fun_ref are what I believe good examples of currying right out from the standard library. The latter two curry the hidden *this pointer. Ben Dec 11 '05 #11

 P: n/a benben wrote: I believe function objects would be more appropriate as they can hold a state (the extra argument) Yes. You are right. I was trying hard to simulate currying using plane old functions but could not get how to do that int (*fp)(char); fp f(int x) { //somehow create a function and store this x in it so that when that function will be invoked later in the code we could get back this x. // could be done with global variables but would require a fresh global variable for every invocation of f. // best alternative is to use function objects } Dec 11 '05 #12

 P: n/a Protoman wrote: THIS IS NOT HOMEWORK!!!!!!! I AM A COLLEGE MED STUDENT!!!! THIS IS A BLOODY HOBBY OF MINE!!!! NOTHING I EVER POST IS HOMEWORK!!!!! I thought you were a JPL Rocket Scientist? Dec 11 '05 #13

 P: n/a "red floyd" wrote in message news:4i*******************@newssvr14.news.prodigy. com Protoman wrote: THIS IS NOT HOMEWORK!!!!!!! I AM A COLLEGE MED STUDENT!!!! THIS IS A BLOODY HOBBY OF MINE!!!! NOTHING I EVER POST IS HOMEWORK!!!!! I thought you were a JPL Rocket Scientist? I remember now. Protoman is a liar who just tells one story after another. I had him in my blocked senders list, but I haven't transferred that list over to my new computer. Oh well, I have corrected that now, at least as far as Protoman is concerned. -- John Carson Dec 11 '05 #14

 P: n/a No, this is the *truth*. I've just finished pre-med at UCLA and I'm entering Harvard med. Now, lets get back on topic. I DON'T LIE ANYMORE!!!! Thank you. Dec 11 '05 #15

### This discussion thread is closed

Replies have been disabled for this discussion.