我想在lambda表达式中调用我的类的方法:
- void my_class::my_method(my_obj& obj)
- {
- }
- void my_class::test_lambda()
- {
- std::list<my_obj> my_list;
- std::for_each(my_list.begin(),my_list.end(),[](my_obj& obj)
- {
- // Here I want to call my_method:
- // my_method(obj);
- });
- }
我能怎么做?
解决方法
您需要明确或隐含地捕获这些:
- std::for_each(l.begin(),l.end(),[this](my_obj& o){ // or [=] or [&]
- my_method(o); // can be called as if the lambda was a member
- });