F
flopbucket
Hi,
I am having some problem using std::bind1st() and mem_fun. I want to
bind member function calls to some kind of functor so it can be called
later.
The following works fine for me:
class Foo
{
std::string name;
public:
Foo() : name("mike") {}
std::string getName()
{
return name;
}
void setName(std::string n)
{
name = n;
}
};
typedef std::binder1st<std::mem_fun1_t<void, Foo, std::string > >
SetterFunc;
main()
{
Foo foo;
SetterFunc f = std::bind1st(mem_fun(&Foo::setName), &foo);
std::string test("testing");
f(test);
std::cout << "after call, name is " << foo.getName() <<
std::endl;
}
But when I want to bind the getName() method, I have lots of trouble:
typedef std::binder1st<std::mem_fun_t<std::string, Foo > > GetterFunc;
GetterFunc y = std::bind1st(mem_func(&Foo::getName), &foo);
I think I don't want to use bind1st because the getName() method takes
no parameters? Is there a std::bind() ?
Also, what if setName() takes "const std::string&" instead of just
"std::string" ? Do I need mem_fun_ref or some const variation of it?
Thanks for any tips.
I am having some problem using std::bind1st() and mem_fun. I want to
bind member function calls to some kind of functor so it can be called
later.
The following works fine for me:
class Foo
{
std::string name;
public:
Foo() : name("mike") {}
std::string getName()
{
return name;
}
void setName(std::string n)
{
name = n;
}
};
typedef std::binder1st<std::mem_fun1_t<void, Foo, std::string > >
SetterFunc;
main()
{
Foo foo;
SetterFunc f = std::bind1st(mem_fun(&Foo::setName), &foo);
std::string test("testing");
f(test);
std::cout << "after call, name is " << foo.getName() <<
std::endl;
}
But when I want to bind the getName() method, I have lots of trouble:
typedef std::binder1st<std::mem_fun_t<std::string, Foo > > GetterFunc;
GetterFunc y = std::bind1st(mem_func(&Foo::getName), &foo);
I think I don't want to use bind1st because the getName() method takes
no parameters? Is there a std::bind() ?
Also, what if setName() takes "const std::string&" instead of just
"std::string" ? Do I need mem_fun_ref or some const variation of it?
Thanks for any tips.