Function returning a function pointer?

P

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!!!
 
B

benben

Protoman said:
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
 
J

John Carson

Protoman said:
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.
 
A

Alf P. Steinbach

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

Protoman

THIS IS NOT HOMEWORK!!!!!!! I AM A COLLEGE MED STUDENT!!!! THIS IS A
BLOODY HOBBY OF MINE!!!! NOTHING I EVER POST IS HOMEWORK!!!!!
 
N

Neelesh Bodas

Protoman said:
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
 
P

Protoman

Neelesh said:
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!!!
 
B

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.

Ben
 
B

benben

Neelesh said:
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
 
N

Neelesh Bodas

benben said:
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
}
 
R

red floyd

Protoman said:
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?
 
J

John Carson

red floyd said:
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.
 
P

Protoman

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top