Function returning a function pointer?

Discussion in 'C++' started by Protoman, Dec 11, 2005.

  1. Protoman

    Protoman Guest

    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!!!
     
    Protoman, Dec 11, 2005
    #1
    1. Advertising

  2. Protoman

    benben Guest

    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
     
    benben, Dec 11, 2005
    #2
    1. Advertising

  3. Protoman

    John Carson Guest

    "Protoman" <> wrote in message
    news:
    > 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
     
    John Carson, Dec 11, 2005
    #3
  4. Protoman

    Protoman Guest

    Can I use such a construct to do currying?
     
    Protoman, Dec 11, 2005
    #4
  5. * 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?
     
    Alf P. Steinbach, Dec 11, 2005
    #5
  6. Protoman

    Protoman Guest

    THIS IS NOT HOMEWORK!!!!!!! I AM A COLLEGE MED STUDENT!!!! THIS IS A
    BLOODY HOBBY OF MINE!!!! NOTHING I EVER POST IS HOMEWORK!!!!!
     
    Protoman, Dec 11, 2005
    #6
  7. 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
     
    Neelesh Bodas, Dec 11, 2005
    #7
  8. Protoman

    Protoman Guest

    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!!!
     
    Protoman, Dec 11, 2005
    #8
  9. * 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?
     
    Alf P. Steinbach, Dec 11, 2005
    #9
  10. Protoman

    benben Guest

    > 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
     
    benben, Dec 11, 2005
    #10
  11. Protoman

    benben Guest

    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
     
    benben, Dec 11, 2005
    #11
  12. 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
    }
     
    Neelesh Bodas, Dec 11, 2005
    #12
  13. Protoman

    red floyd Guest

    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?
     
    red floyd, Dec 11, 2005
    #13
  14. Protoman

    John Carson Guest

    "red floyd" <> wrote in message
    news:4iRmf.39460$
    > 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
     
    John Carson, Dec 11, 2005
    #14
  15. Protoman

    Protoman Guest

    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.
     
    Protoman, Dec 11, 2005
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    744
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    876
    Jordan Abel
    Mar 28, 2006
  3. Mark Piffer
    Replies:
    9
    Views:
    941
    luserXtrog
    May 15, 2009
  4. Replies:
    4
    Views:
    1,330
    Fred Zwarts
    Jul 2, 2009
  5. pembed2012
    Replies:
    1
    Views:
    475
    James Kuyper
    Feb 27, 2012
Loading...

Share This Page