A function that returns a pointer on a function

Discussion in 'C++' started by Boris Sargos, Apr 23, 2004.

  1. Boris Sargos

    Boris Sargos Guest

    Hi,

    I need to write a function that returns a pointer on a function. Is it
    possible, and which is the syntax ?

    Thanks.
    Boris
     
    Boris Sargos, Apr 23, 2004
    #1
    1. Advertising

  2. "Boris Sargos" <> wrote in message
    news:c6ag98$dkg$...
    > Hi,
    >
    > I need to write a function that returns a pointer on a function. Is it
    > possible, and which is the syntax ?
    >
    > Thanks.
    > Boris
    >


    Trying to do that without using a typedef is a real test of your
    understanding of type declarations. The easy way is with a typedef

    typedef void (*FUNC_PTR)(void); // or whatever

    FUNC_PTR some_function()
    {
    ...
    }

    It is however impossible to define a function that returns a pointer to
    itself, that would mean an infinite recursion in the type of that function.

    john
     
    John Harrison, Apr 23, 2004
    #2
    1. Advertising

  3. Boris Sargos

    Siemel Naran Guest

    "John Harrison" <> wrote in message
    news:c6ahbn$9ppkf$1@ID-> "Boris Sargos" <> wrote in
    message

    > > I need to write a function that returns a pointer on a function. Is it
    > > possible, and which is the syntax ?


    > Trying to do that without using a typedef is a real test of your
    > understanding of type declarations. The easy way is with a typedef


    Without typedef I think this is it:

    void (*some_function())();

    This is a function some_function taking no arguments and returning a pointer
    to a function taking no arguments and returning a void. It's similar to a
    function taking a reference to an array of elements. But of course, what
    you have below is cleaner:

    > typedef void (*FUNC_PTR)(void); // or whatever
    >
    > FUNC_PTR some_function()
    > {
    > ...
    > }
    >
    > It is however impossible to define a function that returns a pointer to
    > itself, that would mean an infinite recursion in the type of that

    function.

    This makes sense, because of

    typedef FUNC_PTR (*FUNC_PTR)(void); // or whatever

    But what about

    void (*)(*some_function())();

    Just guessing. Could be wrong.
     
    Siemel Naran, Apr 23, 2004
    #3
  4. "Siemel Naran" <> wrote in message
    news:c7cic.28057$...
    > "John Harrison" <> wrote in message
    > news:c6ahbn$9ppkf$1@ID-> "Boris Sargos" <> wrote in
    > message
    >
    > > > I need to write a function that returns a pointer on a function. Is it
    > > > possible, and which is the syntax ?

    >
    > > Trying to do that without using a typedef is a real test of your
    > > understanding of type declarations. The easy way is with a typedef

    >
    > Without typedef I think this is it:
    >
    > void (*some_function())();
    >


    That's right.

    > This is a function some_function taking no arguments and returning a

    pointer
    > to a function taking no arguments and returning a void. It's similar to a
    > function taking a reference to an array of elements. But of course, what
    > you have below is cleaner:
    >
    > > typedef void (*FUNC_PTR)(void); // or whatever
    > >
    > > FUNC_PTR some_function()
    > > {
    > > ...
    > > }
    > >
    > > It is however impossible to define a function that returns a pointer to
    > > itself, that would mean an infinite recursion in the type of that

    > function.
    >
    > This makes sense, because of
    >
    > typedef FUNC_PTR (*FUNC_PTR)(void); // or whatever
    >
    > But what about
    >
    > void (*)(*some_function())();
    >
    > Just guessing. Could be wrong.
    >


    Syntax error on my compiler. If it's not possible with a typedef I don't see
    why you think it might be possible without a typedef.

    john

    >
     
    John Harrison, Apr 23, 2004
    #4
    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. vizlab
    Replies:
    3
    Views:
    4,273
    Michael Bar-Sinai
    Oct 17, 2007
  2. sqweek
    Replies:
    2
    Views:
    430
    Charles Rapp
    Jan 13, 2006
  3. ++imanshu
    Replies:
    7
    Views:
    499
    ++imanshu
    Aug 23, 2008
  4. Trans
    Replies:
    2
    Views:
    251
    Trans
    Nov 6, 2005
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    674
    David A. Black
    Jul 2, 2008
Loading...

Share This Page