returning a void (*)(void)

Discussion in 'C++' started by Sergio, Jan 5, 2005.

  1. Sergio

    Sergio Guest

    If I have a private member:

    void (*func)(void);

    how can i declare a 'get' function that returns it? I tryed:

    void (*)()GetFunc()
    {
    return func;
    }

    but looks like that's not the way...

    thanks
     
    Sergio, Jan 5, 2005
    #1
    1. Advertising

  2. On 5 Jan 2005 03:57:37 -0800, "Sergio" <> wrote:

    >If I have a private member:
    >
    >void (*func)(void);
    >
    >how can i declare a 'get' function that returns it? I tryed:
    >
    >void (*)()GetFunc()
    >{
    > return func;
    >}
    >
    >but looks like that's not the way...
    >
    >thanks


    It's easier with a typedef:

    typedef void (* func_t)();

    struct C {
    func_t func;
    func_t getFunc() { return func; }
    };

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jan 5, 2005
    #2
    1. Advertising

  3. Sergio wrote:
    > If I have a private member:
    >
    > void (*func)(void);


    Note that this signature matches a member only if the member
    is static: otherwise the type is more something like this

    | void (C::*func)(void);

    where 'C' is the class containing the member.

    > how can i declare a 'get' function that returns it? I tryed:
    >
    > void (*)()GetFunc()


    | void (*GetFunc())()

    is the syntax for a void function returning void.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
     
    Dietmar Kuehl, Jan 5, 2005
    #3
  4. Sergio

    owl ling Guest

    well, yoiu should writen as the following code.
    typedef void (*FUNC)(void);

    void Func(void)
    {
    cout << "func" << endl;
    }
    FUNC GetFunc()
    {
    FUNC fp = Func;
    return fp;
    }
     
    owl ling, Jan 5, 2005
    #4
  5. Sergio

    msalters Guest

    Sergio wrote:
    > If I have a private member:
    >
    > void (*func)(void);
    >
    > how can i declare a 'get' function that returns it? I tryed:
    >
    > void (*)()GetFunc()
    > {
    > return func;
    > }
    >
    > but looks like that's not the way...


    Don't try it this way, use a typedef.

    typedef void(*fun_void_void)();
    fun_void_void func;
    fun_void_void GetFunc() { return func; }
    HTH,
    Michiel Salters
     
    msalters, Jan 5, 2005
    #5
  6. Sergio

    Old Wolf Guest

    msalters wrote:
    > Sergio wrote:
    > > If I have a private member:
    > >
    > > void (*func)(void);
    > > how can i declare a 'get' function that returns it? I tryed:

    >
    > Don't try it this way, use a typedef.
    >
    > typedef void(*fun_void_void)();
    > fun_void_void func;
    > fun_void_void GetFunc() { return func; }


    Another option is to typedef the function type (rather than the
    pointer-to-function). I only mention this because it hadn't
    occurred to me that it was possible until recently, and I prefer
    to avoid pointer typedefs if I can:

    | typedef void (fun_void_void) ();
    | fun_void_void func; // this declares a function
    | fun_void_void *ptr_func = func;
    | fun_void_void * GetFunc() { return ptr_func; }

    or

    | fun_void_void * GetFunc() { return func; }

    because the name of a function is converted to a pointer to
    that function, in a value context.
     
    Old Wolf, Jan 5, 2005
    #6
  7. Sergio wrote:
    > If I have a private member:
    >
    > void (*func)(void);
    >
    > how can i declare a 'get' function that returns it? I tryed:


    On more way:

    #include <boost/mpl/identity.hpp>

    using namespace boost::mpl;

    identity<void (*)(void)>::type get();
     
    Jonathan Turkanis, Jan 5, 2005
    #7
    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. Ollej Reemt
    Replies:
    7
    Views:
    605
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    841
    The Real OS/2 Guy
    Oct 28, 2003
  3. David M. Wilson
    Replies:
    8
    Views:
    505
    Ben Pfaff
    Jan 7, 2004
  4. Replies:
    5
    Views:
    886
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    439
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page