473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing an object's method as a callback

I often find that I need to pass a non-static method as a call-back
instead of a functor or a function. The problem with that is the
implicit 'this' parameter. So I created an adaptor that given an
object and a unary method creates a unary_function-defived functor
bound to that object and method. Code is below.

// Method-to-functor adapter
template<class TClass, typename TArg, typename TRet>
class unary_method : public unary_function<TArg, TRet>
{
public:
unary_method(TClass& object,
TRet (TClass::*method)(TArg)) :
_object(object),
_method(method)
{}

TRet operator()(TArg arg) {
(_object.*_method)(arg);
}

private:
TClass& _object;
TRet (TClass::*_method)(TArg);
};

Questions:
Is this the best (simplest, most efficient) way to do it?
Isn't this a common enough problem for the solution to be available? I
searched, but found nothing in stl, boost or anywhere else.

Here's a test driver, in case you want to give it a try:
<code>
#include <iostream>
#include <functional>

using namespace std;

// Method-to-functor adapter
template<class TClass, typename TArg, typename TRet>
class unary_method : public unary_function<TArg, TRet>
{
public:
unary_method(TClass& object,
TRet (TClass::*method)(TArg)) :
_object(object),
_method(method)
{}

TRet operator()(TArg arg) {
(_object.*_method)(arg);
}

private:
TClass& _object;
TRet (TClass::*_method)(TArg);
};
class MyClass
{
public:
void MyMethod(int a) {
cout << a << endl;
};

static void MyStaticMethod(int a) {
cout << a << endl;
};
};
main () {
void (*foo)(int) = &MyClass::MyStaticMethod;
foo(3);

MyClass my_object;
unary_method<MyClass, int, voidbar(my_object,
&MyClass::MyMethod);
bar(5);
}
</code>

Regards,
Zori

Aug 10 '06 #1
2 3893
zz*****@gmail.com wrote:
I often find that I need to pass a non-static method as a call-back
instead of a functor or a function. The problem with that is the
implicit 'this' parameter. So I created an adaptor that given an
object and a unary method creates a unary_function-defived functor
bound to that object and method. Code is below.

[code redacted]

Questions:
Is this the best (simplest, most efficient) way to do it?
Isn't this a common enough problem for the solution to be available? I
searched, but found nothing in stl, boost or anywhere else.
have you looked at std::mem_fun and std::mem_fun_ptr?
Aug 10 '06 #2
zz*****@gmail.com schrieb:
I often find that I need to pass a non-static method as a call-back
instead of a functor or a function. The problem with that is the
implicit 'this' parameter. So I created an adaptor that given an
object and a unary method creates a unary_function-defived functor
bound to that object and method. Code is below.

// Method-to-functor adapter
[...]
Questions:
Is this the best (simplest, most efficient) way to do it?
Isn't this a common enough problem for the solution to be available? I
searched, but found nothing in stl, boost or anywhere else.
boost::bind and boost::function, which will be in the next standard.

--
Thomas
Aug 10 '06 #3

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

Similar topics

1
by: Philippe C. Martin | last post by:
Hi, I have he following need and do not find an easy way out: I have many menu items and would like them all to call the same method -However, I need the method called to react differently...
0
by: andreas | last post by:
Hi there, I have the following situation: 1. DOT.NET Application is started 2. DOT.NET Application instantiates Access.Application.8 3. Opens a specified database (MDB) 4. DOT.NET...
2
by: Nick | last post by:
Is there a way that if I host my remoted object in IIS (not having to mess with encryption & authentication via a custom sink) that the server can raise events and the clients can detect them? If...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
0
by: Jeffrey B. Holtz | last post by:
Has anyone used the multimedia timere timeSetEvent in C#? I'm trying to use it to get a 1ms accurate timer. All the other implementations are far to inaccurate in their resolution. 1ms Timer =...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
3
by: Malcolm | last post by:
Hi. I am attempting something new in my coding and need some direction. I have a web service that I am attempting to return a custom colleciton of custom objects of a single type. When I call...
2
by: TN | last post by:
I have a bit of C code, that creates an instance of a .Net class that has been built as a type library. Everything is working as expected, I can pass strings to methods in the object. What I...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
0
bartonc
by: bartonc | last post by:
I'm working on a scheme which reduces the interface between classed to a single method and a list messages that the co-operative class knows. I invite all comments and/or critiques/improvements: We...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.