fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class A {
  6. public:
  7. int value;
  8. };
  9.  
  10.  
  11. // A reference passed by value - reassign the value.
  12. void myValFunction(A* value) {
  13. A other;
  14. other.value = 4;
  15. value = &other;
  16. }
  17.  
  18. // A reference passed by value - update its value.
  19. void myUpdateValFunction(A* value) {
  20. value->value = 5;
  21. }
  22.  
  23. // Pass by reference - reassign the reference.
  24. // You can't do this in JavaScript
  25. void myRefFunction(A& ref) {
  26. A other;
  27. other.value = 6;
  28. ref = other;
  29. }
  30.  
  31. int main() {
  32. // I'll create 3 A instances here (a#) and their references (r#)
  33. // In JavaScript, we only really interact with the r# value.
  34. A a1; A* r1 = &a1;
  35. A a2; A* r2 = &a2;
  36. A a3; A* r3 = &a3;
  37.  
  38. // Set some values through the reference.
  39. r1->value = 1;
  40. r2->value = 2;
  41. r3->value = 3;
  42.  
  43.  
  44. myValFunction(r1);
  45. myUpdateValFunction(r2);
  46.  
  47. myRefFunction(a3);
  48.  
  49. std::cout << r1->value << std::endl;
  50. std::cout << r2->value << std::endl;
  51. std::cout << r3->value << std::endl;
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
1
5
6