M
ma740988
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
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