Just out of curiosity, is there a way to get write access to member variables via boost::bind
? I can get it via boost::multi_index::member
, but just want to know other methods as well.
Example:
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/multi_index/member.hpp>
using namespace std;
struct Test
{
string name;
Test(const string &name)
: name(name)
{ }
};
int main()
{
Test test("Bob");
boost::multi_index::member<Test, string, &Test::name> nameMember;
string &ref = nameMember(test);
cout << ref << "\n";
// Write Access
ref = "Tim";
// Read-only Access
boost::function<const string& (Test*)> nameGetter = boost::bind(&Test::name, _1);
cout << nameGetter(&test) << "\n";
return 0;
}
Output:
Bob
Tim
Yes, it is possible:
// Read-write Access
boost::function<string&(Test*)> nameSetter =
boost::bind<std::string&>(&Test::name, _1);
nameSetter(&test) = "test";
cout << ref << "\n";