Advertisement
mario_mos

mario_lambda

Sep 1st, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #ifndef TEST_H
  2. #define TEST_H
  3.  
  4.  
  5. class test
  6. {
  7. public:
  8.     test();
  9.     int lambda2();
  10. private:
  11.     int m_i = 4;
  12.     int m_j = 3;
  13. };
  14.  
  15. #endif // TEST_H
  16.  
  17. #include "test.h"
  18. #include <iostream>
  19. test::test() {
  20.     auto lambda = [this] { return m_i; };
  21.     std::cout << lambda() << std::endl;
  22. }
  23.  
  24. int test::lambda2()
  25. {
  26.     auto lambda2 =  [this](int a) { return m_i + a; };
  27.     std::cout << lambda2(2) << std::endl;
  28. }
  29.  
  30.  
  31. #include <iostream>
  32. #include "test.h"
  33. using namespace std;
  34.  
  35. int main()
  36. {
  37.     int i = 5;
  38.     int j = 6;
  39.     auto p = [&] (int& a , int& b) { cout << a+b << endl; a++; };
  40.     p(i,j); // Appel de la fonction
  41.     cout << i << " " << j << endl;
  42.  
  43.     test obj1;
  44.  
  45.     obj1.lambda2();
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement