- 论坛徽章:
- 14
|
为什么要加 typedef ?
template <typename R, typename T, typename Arg>
class simple_bind_t
{
private:
typedef R (T::*fn)(Arg);
fn fn_;
T t_;
public:
simple_bind_t( fn f, const T& t ) : fn_(f), t_(t)
{
}
R operator()(Arg& a)
{
return (t_.*fn_)(a);
}
};
template <typename R, typename T, typename Arg>
simple_bind_t<R,T,Arg> simple_bind( R (T::*fn)(Arg), const T& t )
{
return simple_bind_t<R,T,Arg>(fn,t);
}
// test
struct foo
{
public:
foo( int val=0 ) : val_(val)
{
}
int bar( int x )
{
return val_ + x;
}
private:
int val_;
};
#include <iostream>
int main(void)
{
foo a(100);
int b = 50;
int c = simple_bind( &foo::bar, a )( b );
std::cout << c << std::endl;
return 0;
} |
|