468,513 Members | 1,759 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 3667
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Philippe C. Martin | last post: by
reply views Thread by andreas | last post: by
2 posts views Thread by Nick | last post: by
6 posts views Thread by keepyourstupidspam | last post: by
reply views Thread by Jeffrey B. Holtz | last post: by
bartonc
reply views Thread by bartonc | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.