Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function returning a function pointer?

Reply
Thread Tools

Function returning a function pointer?

 
 
Protoman
Guest
Posts: n/a
 
      12-11-2005
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!!!

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      12-11-2005
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

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      12-11-2005
"Protoman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      12-11-2005
Can I use such a construct to do currying?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-11-2005
* 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?
 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      12-11-2005
THIS IS NOT HOMEWORK!!!!!!! I AM A COLLEGE MED STUDENT!!!! THIS IS A
BLOODY HOBBY OF MINE!!!! NOTHING I EVER POST IS HOMEWORK!!!!!

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-11-2005

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

 
Reply With Quote
 
Protoman
Guest
Posts: n/a
 
      12-11-2005

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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-11-2005
* 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?
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      12-11-2005
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
Declaring static function returning pointer to extern function pembed2012 C Programming 1 02-27-2012 08:21 PM
function returning function pointer (recursive type) Mark Piffer C Programming 9 05-15-2009 07:54 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments