473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking member functions of objects


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

Apr 6 '06 #1
4 1729
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

Apr 6 '06 #2

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.*functio n(); }
};

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.*functio n(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.*functio n(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...

Apr 6 '06 #3
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.*functio n(); }
};

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.*functio n(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.*functio n(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

Apr 7 '06 #4

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/

Apr 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2022
by: college | last post by:
I am trying to pass an array of user objects in session and take the array out of session and call the member functions of each object. However I am getting a Fatal error: Call to a member function on a non-object. I have session.autostart turned off, and I have the class definition for those objects included before the call to session_start(). I am on PHP 4.2.3. Is this possible to do? While debugging, it appears that the objects in...
2
2835
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the dependency/independency on the lifecycle of the object, what is the significant differences between public member functions and public static member functions?
5
9367
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
6
1632
by: Elven | last post by:
Hi! I've been trying to figure this out, but can't seem to find an answer. Most likely it's some weird bug in my code, but since I don't have access to the Standard, I wanted to cover my bases.. basically the situation is that I'm writing a logging facility, class Log. Instances of the class can be invoked to read a message by using the method Read() that enables the overloaded op<<. The trick is, Read() does not necessarily return the...
3
1981
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
22
2753
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
39
3175
by: utab | last post by:
Dear all, Is there a clear distinction how to decide which functions to be members of a class and which not How is your attitude (Your general way from your experiences ...) "If the function changes the state of the object, it ought to be a member of that object." Reference Accelerated C++, A. Koenig, page 159.
2
1924
by: Angus | last post by:
I have a member function, int GetLogLevel() which I thought I should change to int GetLogLevel() const - I made the change and it works fine. But in the function I am creating buffers and of course the buffers are filling up with data. So some variable values are changing. So what is rule for a const member function? Is it that only member variables cannot change? But local variables inside the function can?
3
2816
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member function pointer array (Handlers) - compiler doesnt let me do that. I got an error as below: "invalid use of non-static member function bool test::func1(long int)"
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.