I am learning boost lambda (not c++0X lambda because I guess they are different). But I can't find a way online to call a member function (and then output the result) if the only input parameter is a call object. I mean this line works:
for_each(vecCt.begin(), vecCt.end(), cout<<_1<<endl);
if vecCt is a vector
of int
. But what if vecCt is a vector
of MyClass
, which has a function called getName
to return a string? Neither this:
for_each(vecCt.begin(), vecCt.end(), cout<<_1->getName());
nor this:
for_each(vecCt.begin(), vecCt.end(), cout<<*_1.getName());
works.
I searched online but many results suggest to use bind when calling member function. Now I know this
for_each(vecCt.begin(), vecCt.end(), bind(&MyClass::getName, _1);
makes me able to call getName
on each object passed int, but how can I pass this output to cout? This doesn't work:
for_each(vecCt.begin(), vecCt.end(), cout<<bind(&MyClass::.getName, _1);