returning a void (*)(void)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sergio

    returning a void (*)(void)

    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

  • Bob Hairgrove

    #2
    Re: returning a void (*)(void)

    On 5 Jan 2005 03:57:37 -0800, "Sergio" <sergiozin@gmai l.com> wrote:
    [color=blue]
    >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[/color]

    It's easier with a typedef:

    typedef void (* func_t)();

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

    --
    Bob Hairgrove
    NoSpamPlease@Ho me.com

    Comment

    • Dietmar Kuehl

      #3
      Re: returning a void (*)(void)

      Sergio wrote:[color=blue]
      > If I have a private member:
      >
      > void (*func)(void);[/color]

      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.
      [color=blue]
      > how can i declare a 'get' function that returns it? I tryed:
      >
      > void (*)()GetFunc()[/color]

      | void (*GetFunc())()

      is the syntax for a void function returning void.
      --
      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
      <http://www.contendix.c om> - Software Development & Consulting

      Comment

      • owl ling

        #4
        Re: returning a void (*)(void)

        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;
        }

        Comment

        • msalters

          #5
          Re: returning a void (*)(void)


          Sergio wrote:[color=blue]
          > 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...[/color]

          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

          Comment

          • Old Wolf

            #6
            Re: returning a void (*)(void)

            msalters wrote:[color=blue]
            > Sergio wrote:[color=green]
            > > If I have a private member:
            > >
            > > void (*func)(void);
            > > how can i declare a 'get' function that returns it? I tryed:[/color]
            >
            > Don't try it this way, use a typedef.
            >
            > typedef void(*fun_void_ void)();
            > fun_void_void func;
            > fun_void_void GetFunc() { return func; }[/color]

            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.

            Comment

            • Jonathan Turkanis

              #7
              Re: returning a void (*)(void)

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

              On more way:

              #include <boost/mpl/identity.hpp>

              using namespace boost::mpl;

              identity<void (*)(void)>::typ e get();


              Comment

              Working...