Member Callback Functions

Discussion in 'C++' started by cadull, May 6, 2005.

  1. cadull

    cadull Guest

    The aim of the following code is to store and call member functions on an
    object.

    class C {};

    class CF
    {
    public:
    template<typename T> inline CF(T& o, void (T::*f)())
    : pO(&o), pF( (void (C::*)()) f) {};

    inline void Call() { (pO->*pF)(); };

    void (C::*pF)();
    C* pO;
    };

    class A : public C
    {
    public:
    void F() {};
    };

    int main()
    {
    A a;
    CF f(a, A::F);
    f.Call();
    }

    Objects of type CF will be stored in an array (or container) and may call
    functions from different classes. Performance and memory comparisions with
    virtual functions would depend on usage.

    My question: since class C appears to have little impact other than allowing
    compilation, can this be written without C? What other alternatives are
    there (with type generic storage)?

    Regards,
    cadull
    cadull, May 6, 2005
    #1
    1. Advertising

  2. cadull

    cadull Guest

    Moving the cast up a level (to a structure containing the function pointer)
    removes the need for A to extend C. This provides the interface I'm after.
    Possible improvements?

    class CF
    {
    public:
    class C {};

    template<typename T> class D
    {
    public:
    inline D(T& o, void (T::*f)())
    : pO(&o), pF(f) {};

    void (T::*pF)();
    T* pO;
    };

    template<typename T> inline CF(T& o, void (T::*f)())
    : d( (D<C>&) D<T>(o, f)) {};

    inline void Call() { (d.pO->*d.pF)(); };

    D<C> d;
    };

    class A
    {
    public:
    void F() {};
    };

    int main()
    {
    A a;
    CF f(a, A::F);
    f.Call();
    }

    "cadull" <> wrote in message
    news:d5fhvh$90i$...
    > The aim of the following code is to store and call member functions on an
    > object.
    >
    > class C {};
    >
    > class CF
    > {
    > public:
    > template<typename T> inline CF(T& o, void (T::*f)())
    > : pO(&o), pF( (void (C::*)()) f) {};
    >
    > inline void Call() { (pO->*pF)(); };
    >
    > void (C::*pF)();
    > C* pO;
    > };
    >
    > class A : public C
    > {
    > public:
    > void F() {};
    > };
    >
    > int main()
    > {
    > A a;
    > CF f(a, A::F);
    > f.Call();
    > }
    >
    > Objects of type CF will be stored in an array (or container) and may call
    > functions from different classes. Performance and memory comparisions with
    > virtual functions would depend on usage.
    >
    > My question: since class C appears to have little impact other than
    > allowing compilation, can this be written without C? What other
    > alternatives are there (with type generic storage)?
    >
    > Regards,
    > cadull
    >
    cadull, May 6, 2005
    #2
    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:
    5
    Views:
    518
  2. Hamish
    Replies:
    3
    Views:
    573
    Alf P. Steinbach
    Jan 25, 2008
  3. flounder
    Replies:
    4
    Views:
    507
    flounder
    Apr 2, 2009
  4. Hicham Mouline
    Replies:
    0
    Views:
    431
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    412
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page