Invoking member functions of objects

Discussion in 'C++' started by ma740988, Apr 6, 2006.

  1. ma740988

    ma740988 Guest

    Trying to build on something I picked up from a Scott Meyers.
    Consider:


    class BaseMenuItem {
    public:
    virtual void invoke() const = 0;
    };

    template<class Class, class MemFuncPtr, class Param>
    class MenuItem: public BaseMenuItem {
    private:
    Class& object;
    MemFuncPtr function;
    Param& parameter;


    public:
    MenuItem(Class& obj, MemFuncPtr mfp, Param& p):
    object(obj), function(mfp), parameter(p) {}


    virtual void invoke() const { object.*mf(p); }
    };

    What I'd like to do in the menu object is expand on it to setup a table
    which contains a reference to the object, its member function which
    should be invoked if it's item is selected and argument(s), i.e.

    Item# Object/Member
    ======================================
    1. c1/a/arg1
    2. c2/b/arg1, arg2, arg3
    3. c3/c/arg1, arg2, arg3, arg4

    So if 1 is selected, I invoke c1.a(arg1), 2 is selected, I invoke
    c2.b(arg1, arg2), etc.

    My thougths are to implement classes MenuItem0 .. MenuItem3 which work
    with 0 .. 3 arguments to the function. I'm going around in circles
    trying to implement this. Could use some assistance?

    I suspect an alternative is to use function overloading.

    Thanks
    ma740988, Apr 6, 2006
    #1
    1. Advertising

  2. ma740988

    mlimber Guest

    ma740988 wrote:
    > Trying to build on something I picked up from a Scott Meyers.
    > Consider:
    >
    >
    > class BaseMenuItem {
    > public:
    > virtual void invoke() const = 0;
    > };
    >
    > template<class Class, class MemFuncPtr, class Param>
    > class MenuItem: public BaseMenuItem {
    > private:
    > Class& object;
    > MemFuncPtr function;
    > Param& parameter;
    >
    >
    > public:
    > MenuItem(Class& obj, MemFuncPtr mfp, Param& p):
    > object(obj), function(mfp), parameter(p) {}
    >
    >
    > virtual void invoke() const { object.*mf(p); }
    > };
    >
    > What I'd like to do in the menu object is expand on it to setup a table
    > which contains a reference to the object, its member function which
    > should be invoked if it's item is selected and argument(s), i.e.
    >
    > Item# Object/Member
    > ======================================
    > 1. c1/a/arg1
    > 2. c2/b/arg1, arg2, arg3
    > 3. c3/c/arg1, arg2, arg3, arg4
    >
    > So if 1 is selected, I invoke c1.a(arg1), 2 is selected, I invoke
    > c2.b(arg1, arg2), etc.
    >
    > My thougths are to implement classes MenuItem0 .. MenuItem3 which work
    > with 0 .. 3 arguments to the function. I'm going around in circles
    > trying to implement this. Could use some assistance?
    >
    > I suspect an alternative is to use function overloading.
    >
    > Thanks


    There's no inherent problem in implementing it this way. What is wrong
    exactly? Can you post some code that demonstrates the problem? (BTW,
    you might check out Boost.Bind. It does similar things.)

    Cheers! --M
    mlimber, Apr 6, 2006
    #2
    1. Advertising

  3. ma740988

    ma740988 Guest

    mlimber wrote:
    [....]

    > > Item# Object/Member
    > > ======================================
    > > 1. c1/a/arg1
    > > 2. c2/b/arg1, arg2, arg3
    > > 3. c3/c/arg1, arg2, arg3, arg4
    > >
    > > So if 1 is selected, I invoke c1.a(arg1), 2 is selected, I invoke
    > > c2.b(arg1, arg2), etc.
    > >
    > > My thougths are to implement classes MenuItem0 .. MenuItem3 which work
    > > with 0 .. 3 arguments to the function. I'm going around in circles
    > > trying to implement this. Could use some assistance?
    > >
    > > I suspect an alternative is to use function overloading.
    > >
    > > Thanks

    >
    > There's no inherent problem in implementing it this way. What is wrong
    > exactly? Can you post some code that demonstrates the problem?


    Right!!.

    Actually, the 'Loki' library would be ideal but what irritates the hell
    out of me is I'm _no kidding_ stuck (just not willing right now to pay
    the difference to upgade to GCC 3.3.2) with a compiler so old that's it
    not worth the effort using these high end beasts of a library. At best
    here's what I came up with.

    class BaseMenuItem {
    public:
    virtual void invoke() const = 0;
    };

    template<class Class, class MemFuncPtr>
    class MenuItem0: public BaseMenuItem {
    private:
    Class& object;
    MemFuncPtr function;
    public:
    MenuItem0(Class& obj, MemFuncPtr mfp):
    object(obj), function(mfp) {}
    virtual void invoke() const { object.*function(); }
    };

    template<class Class, class MemFuncPtr, class Param1>
    class MenuItem1: public BaseMenuItem {
    private:
    Class& object;
    MemFuncPtr function;
    Param1 parameter1;
    public:
    MenuItem1(Class& obj, MemFuncPtr mfp, Param1& p1):
    object(obj), function(mfp), parameter1(p1) {}
    virtual void invoke() const { object.*function(parameter1); }
    };

    template<class Class, class MemFuncPtr, class Param1, class Param2>
    class MenuItem2: public BaseMenuItem {
    private:
    Class& object;
    MemFuncPtr function;
    Param1 parameter1;
    Param2 parameter2;
    public:
    MenuItem2(Class& obj, MemFuncPtr mfp, Param1& p1, Param2 p2):
    object(obj), function(mfp), parameter1(p1), parameter2(p2) {}
    virtual void invoke() const { object.*function(parameter1,
    parameter2); }
    };

    Thank goodness I dont have 50 of those :)

    An alternative I think is to use function overloading.. so I'm
    experimenting. In modern C++ design the Loki library has some fancy
    way (the concept revolves around a factory) of allowing up to 50
    arguments.
    So I'm trying to see if function overloading will simplify that...
    ma740988, Apr 6, 2006
    #3
  4. ma740988

    mlimber Guest

    ma740988 wrote:
    > Actually, the 'Loki' library would be ideal but what irritates the hell
    > out of me is I'm _no kidding_ stuck (just not willing right now to pay
    > the difference to upgade to GCC 3.3.2) with a compiler so old that's it
    > not worth the effort using these high end beasts of a library. At best
    > here's what I came up with.
    >
    > class BaseMenuItem {
    > public:
    > virtual void invoke() const = 0;
    > };
    >
    > template<class Class, class MemFuncPtr>
    > class MenuItem0: public BaseMenuItem {
    > private:
    > Class& object;
    > MemFuncPtr function;
    > public:
    > MenuItem0(Class& obj, MemFuncPtr mfp):
    > object(obj), function(mfp) {}
    > virtual void invoke() const { object.*function(); }
    > };
    >
    > template<class Class, class MemFuncPtr, class Param1>
    > class MenuItem1: public BaseMenuItem {
    > private:
    > Class& object;
    > MemFuncPtr function;
    > Param1 parameter1;
    > public:
    > MenuItem1(Class& obj, MemFuncPtr mfp, Param1& p1):
    > object(obj), function(mfp), parameter1(p1) {}
    > virtual void invoke() const { object.*function(parameter1); }
    > };
    >
    > template<class Class, class MemFuncPtr, class Param1, class Param2>
    > class MenuItem2: public BaseMenuItem {
    > private:
    > Class& object;
    > MemFuncPtr function;
    > Param1 parameter1;
    > Param2 parameter2;
    > public:
    > MenuItem2(Class& obj, MemFuncPtr mfp, Param1& p1, Param2 p2):
    > object(obj), function(mfp), parameter1(p1), parameter2(p2) {}
    > virtual void invoke() const { object.*function(parameter1,
    > parameter2); }
    > };
    >
    > Thank goodness I dont have 50 of those :)
    >
    > An alternative I think is to use function overloading.. so I'm
    > experimenting. In modern C++ design the Loki library has some fancy
    > way (the concept revolves around a factory) of allowing up to 50
    > arguments.
    > So I'm trying to see if function overloading will simplify that...


    The overloading Loki uses depends on its typelists and partial
    specialization and still involves a non-trivial amount of repetition. I
    don't see any way to do it with overloading without those tricks, but
    you say that you can't use them. (If you could, you'd just use Loki.)

    Cheers! --M
    mlimber, Apr 7, 2006
    #4
  5. ma740988

    ma740988 Guest

    mlimber wrote:
    [...]
    >
    > The overloading Loki uses depends on its typelists and partial
    > specialization and still involves a non-trivial amount of repetition. I
    > don't see any way to do it with overloading without those tricks, but
    > you say that you can't use them. (If you could, you'd just use Loki.)
    >
    > Cheers! --M


    Actually, I'm thinking along the lines of of how C# events are
    modelled.
    I'm struggling through the CUJ article (URL) below to assist me with
    that.

    http://www.cuj.com/documents/s=8009/cuj0209smith/
    ma740988, Apr 7, 2006
    #5
    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. Elven
    Replies:
    6
    Views:
    340
  2. Replies:
    22
    Views:
    1,495
    Marcus Kwok
    Mar 23, 2006
  3. Ramesh
    Replies:
    2
    Views:
    332
    Ramesh
    Jul 30, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    432
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    414
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page