Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1.  
  2.  
  3. class OuterObject
  4. {
  5. public:
  6. int A;
  7. void someMethod()
  8. {
  9. struct Functor
  10. {
  11. void doIt()
  12. {
  13. this->A=10; //this is the callback; set A=10.
  14. }
  15. };
  16. Functor f;
  17. doSomeCallback(f);
  18. }
  19. };
  20.  
  21.  
  22. //the above is a compile error obviously, because "this" refers to the Functor not OuterObject
  23. //here's the solution
  24.  
  25. class OuterObject
  26. {
  27. public:
  28. int A;
  29. void someMethod()
  30. {
  31. auto self=&OuterObject;
  32. struct Functor
  33. {
  34. void doIt()
  35. {
  36. self->A=10; //this is the callback; set A=10.
  37. }
  38. };
  39. Functor f;
  40. doSomeCallback(f);
  41. }
  42. };
  43.  
  44. //So a lot of times in javascript you have code like
  45.  
  46. class OuterObject
  47. {
  48. someMethod()
  49. {
  50. var self=this;
  51. var f=function() { self.A=10; };
  52. doSomeCallback(f);
  53. }
  54. };
  55.  
  56. //where the 'self' is used to contexually rebind this because of javascript dynamically rebinding 'this' to the lambda object inside a lambda definition.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement