[ create a new paste ] login | about

Link: http://codepad.org/Kzxxk6Hp    [ raw code | output | fork ]

C++, pasted on Jan 13:
#include <iostream>

class A {

   public:
      int value;

};

void callByValueMutate(A* a) {
   
   a->value = 1;

}

void callByReferencePtrAssign(A* &a) {

   a = new A;
   a->value = 0;

}

int main() {

   A* a = new A;
   A* b = a;
   a->value = 3;

   cout << "Initial value A:" << a->value << endl;
   cout << "Initial value B:" << b->value << endl;

   callByValueMutate(a);

   cout << "After call by value A:" << a->value << endl;
   cout << "After call by value B:" << b->value << endl;

   callByReferencePtrAssign(a); 

   cout << "After call by reference A:" << a->value << endl;
   cout << "After call by reference B:" << b->value << endl;

   
   
   return 0;
}


Output:
1
2
3
4
5
6
Initial value A:3
Initial value B:3
After call by value A:1
After call by value B:1
After call by reference A:0
After call by reference B:1


Create a new paste based on this one


Comments: