fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4. #include <vector>
  5.  
  6.  
  7. class Test{
  8.  
  9. public:
  10.  
  11. Test() = default;
  12. Test(const Test& other) = delete;
  13. Test& operator=(const Test& other) = delete;
  14. Test(Test&& other) = default;
  15. Test& operator=(Test&& other) = default;
  16.  
  17.  
  18.  
  19. void setFunction(){
  20. auto a = &Test::a;
  21. lambda = [=](Test *t){
  22. (t->*a) = 2;
  23. };
  24. }
  25.  
  26. int callAndReturn(){
  27. lambda(this);
  28. return a;
  29. }
  30.  
  31. private:
  32. std::function<void(Test*)> lambda;
  33. int a = 50;
  34. };
  35.  
  36.  
  37. int main()
  38. {
  39. Test t;
  40. t.setFunction();
  41. std::vector<Test> elements;
  42. elements.push_back(std::move(t));
  43. std::cout << elements[0].callAndReturn() << std::endl;
  44. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
2