Function Pointers To Member Functions

Discussion in 'C++' started by OvErboRed, Apr 26, 2004.

  1. OvErboRed

    OvErboRed Guest

    I know you can do:

    int (MyClass::*ptr)(int) = &MyClass::func;
    cout << (myObj.*ptr)(5);

    to point to a member function of class MyClass. But how would you go about
    making this generic (i.e., not restricted to any specific class, like
    MyClass)? You can probably resort to C function pointers, but I'd rather
    not. Thanks in advance.
     
    OvErboRed, Apr 26, 2004
    #1
    1. Advertising

  2. "OvErboRed" <> wrote...
    > I know you can do:
    >
    > int (MyClass::*ptr)(int) = &MyClass::func;
    > cout << (myObj.*ptr)(5);
    >
    > to point to a member function of class MyClass. But how would you go about
    > making this generic (i.e., not restricted to any specific class, like
    > MyClass)? You can probably resort to C function pointers, but I'd rather
    > not. Thanks in advance.


    You cannot make it generic. Pointers to members are class-specific.
    Period.

    Victor
     
    Victor Bazarov, Apr 26, 2004
    #2
    1. Advertising

  3. OvErboRed

    OvErboRed Guest

    Fine, but is there no other way to pass in member functions as general
    callbacks? (I guess I'm basically trying to get the same effect as
    runnables in Java or delegates in C#, but using function pointers instead
    of classes implementing Runnable or IDelegate.)

    "Victor Bazarov" <> wrote in
    news:jhYic.35240$aQ6.1900352@attbi_s51:

    > You cannot make it generic. Pointers to members are class-specific.
    > Period.
    >
    > Victor
     
    OvErboRed, Apr 26, 2004
    #3
  4. "OvErboRed" <> wrote...
    > Fine, but is there no other way to pass in member functions as general
    > callbacks?


    Usually not. See FAQ.

    > (I guess I'm basically trying to get the same effect as
    > runnables in Java or delegates in C#, but using function pointers instead
    > of classes implementing Runnable or IDelegate.)


    My guess is that it's possible, but you'll be involving templates and
    thus mostly giving up run-time choices.

    >
    > "Victor Bazarov" <> wrote in
    > news:jhYic.35240$aQ6.1900352@attbi_s51:
    >
    > > You cannot make it generic. Pointers to members are class-specific.
    > > Period.
    > >
    > > Victor
     
    Victor Bazarov, Apr 26, 2004
    #4
  5. OvErboRed

    David White Guest

    "OvErboRed" <> wrote in message
    news:Xns94D6A7AFB8D13yangstaoverbored@127.0.0.1...
    > I know you can do:
    >
    > int (MyClass::*ptr)(int) = &MyClass::func;
    > cout << (myObj.*ptr)(5);
    >
    > to point to a member function of class MyClass. But how would you go about
    > making this generic (i.e., not restricted to any specific class, like
    > MyClass)? You can probably resort to C function pointers, but I'd rather
    > not. Thanks in advance.


    You can't. You can try cheating, if you don't mind writing non-C++,
    non-portable code that might stop working on a different compiler, or the
    next version of your current compiler, or might never work at all on certain
    classes, depending on how the compiler implements them. I once had to work
    on a large, complex and highly event-driven application. The compiler at the
    time didn't have templates. I decided that the usefulness of such a generic
    pointer outweighed language and portability issues, and I wrote some nasty
    macros that enabled any class to have a member-function event handler. The
    event manager thought it was always calling a member of class EventHandler,
    which was defined as follows:

    class EventHandler
    {
    };

    In fact, it was calling member functions of a large number of different,
    unrelated classes at different times from the same call statement.

    DW
     
    David White, Apr 26, 2004
    #5
  6. OvErboRed wrote:

    > I know you can do:
    >
    > int (MyClass::*ptr)(int) = &MyClass::func;
    > cout << (myObj.*ptr)(5);
    >
    > to point to a member function of class MyClass. But how would you
    > go about making this generic (i.e., not restricted to any specific
    > class, like MyClass)? You can probably resort to C function
    > pointers, but I'd rather not. Thanks in advance.


    If this is a way, use a base class which all classes inherit of.
    Virtual methods will grant you access to the methods of the derived
    classes, too.

    Bernhard
     
    Bernhard Holzmayer, Apr 26, 2004
    #6
  7. > "Victor Bazarov" <> wrote in
    > news:jhYic.35240$aQ6.1900352@attbi_s51:
    >
    > > You cannot make it generic. Pointers to members are class-specific.
    > > Period.
    > >
    > > Victor


    NB: OvErboRed, please do not top-post (I moved your reply down).
    "OvErboRed" <> wrote in message
    news:Xns94D6B2465DAFAyangstaoverbored@127.0.0.1...
    > Fine, but is there no other way to pass in member functions as general
    > callbacks? (I guess I'm basically trying to get the same effect as
    > runnables in Java or delegates in C#, but using function pointers instead
    > of classes implementing Runnable or IDelegate.)


    Such a 'generalized callback' can be implemented as a library in C++.
    boost.org, a peer-reviewed collection of C++ libraries, provides
    libraries that do what you are asking for, but it is somewhat complex
    and spread out among multiple compoents. The result, however, is
    relatively simple -- as demonstrated by the example at:
    http://www.boost.org/libs/bind/bind.html#with_boost_function
    See the doc of boost::bind and boost::function for more info.
    http://www.boost.org/doc/html/function.html

    Note that these utilities are likely to be integrated into the
    next revision of the C++ standard, and are even already supported
    by some compiler vendor(s) as documented in this technical report:
    http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1540.pdf


    Also of potential interest is the related boost signals library:
    http://www.boost.org/doc/html/signals.html



    hth,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
     
    Ivan Vecerina, Apr 26, 2004
    #7
  8. OvErboRed <> wrote in message news:<Xns94D6A7AFB8D13yangstaoverbored@127.0.0.1>...
    > I know you can do:
    >
    > int (MyClass::*ptr)(int) = &MyClass::func;
    > cout << (myObj.*ptr)(5);
    >
    > to point to a member function of class MyClass. But how would you go about
    > making this generic (i.e., not restricted to any specific class, like
    > MyClass)? You can probably resort to C function pointers, but I'd rather
    > not. Thanks in advance.


    No. The MyClass part, which maps to the 'this' pointer in the method,
    behaves like a hidden first argument. Therefore, you can't have a single
    pointer type, just as there is no single function pointer type for both
    'void foo(MyClass&)' and 'void bar(YourClass&)'.

    Besides, how would you invoke it? If you have such a generic pointer,
    and you initialize it with &MyClass::foo, and I invoke it on
    std::string s;, what happens?

    That said, you could use a union of funtion pointers.
    e.g.
    union Foo_or_Bar_MP {
    void (Foo::*fooMethod)();
    void (Bar::*barMethod)();
    };
    You must ensure that fooMethod is initialized when you
    call pMyFoo->*(method.fooMethod). How you do that is up to you,
    but it is legal and will work everywhere.

    Regards,
    Michiel Salters
     
    Michiel Salters, Apr 26, 2004
    #8
    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. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    499
    n2xssvv g02gfr12930
    Nov 27, 2005
  2. Hamish
    Replies:
    3
    Views:
    605
    Alf P. Steinbach
    Jan 25, 2008
  3. Hicham Mouline
    Replies:
    0
    Views:
    454
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    441
    Michael DOUBEZ
    Apr 24, 2009
  5. paul
    Replies:
    8
    Views:
    744
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page