fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class NAME
  5. {
  6. public:
  7. int value;
  8.  
  9. void f()
  10. {
  11. std::cout << "f() this=" << static_cast<void*>(this) << " value=" << value << std::endl;
  12. }
  13.  
  14. NAME() : value(12345)
  15. {
  16. std::cout << "default constructor, this=" << static_cast<void*>(this) << std::endl;
  17. }
  18.  
  19. NAME(const NAME &src) : value(src.value)
  20. {
  21. std::cout << "copy constructor, src=" << static_cast<const void*>(&src) << " this=" << static_cast<void*>(this) << std::endl;
  22. }
  23. };
  24.  
  25. int main()
  26. {
  27. std::cout << "Hello World" << std::endl;
  28.  
  29. NAME n;
  30. std::cout << "&n=" << static_cast<void*>(&n) << std::endl;
  31.  
  32. std::function<void ()> callable = std::bind(&NAME::f, &n);
  33. callable();
  34.  
  35. std::function<void ()> callable2 = std::bind(&NAME::f, n);
  36. callable2();
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5640KB
stdin
Standard input is empty
stdout
Hello World
default constructor, this=0x7ffcd8bc4a7c
&n=0x7ffcd8bc4a7c
f() this=0x7ffcd8bc4a7c value=12345
copy constructor, src=0x7ffcd8bc4a7c this=0x7ffcd8bc4a90
copy constructor, src=0x7ffcd8bc4a90 this=0x559cddf12eb0
f() this=0x559cddf12eb0 value=12345