+ 1

What is the output of this code? Why? What does '&' mean when used to declare an 'int' value?

void f(int& x) { cout << x++; x = 2; } int main(){ int a = 3; int& b= a; f(a); cout << b; }

10th Oct 2017, 10:00 AM
Pablo Bustamante
Pablo Bustamante - avatar
1 Réponse
+ 15
#include <iostream> using namespace std; void f(int& x) { // 3. Every change on x variable will affect a // 4. print the current value of x (an allias for a), then increment by 1 cout << x++; // 3 // 5. put 2 in x (and a also) x = 2; } int main() { int a = 3; // 1. define and initialize an integer reference using a variable int &b= a; // 2. calling f function with a variable f(a); // 6. print the value of b which is an alias for a cout << b; // 2 } Output : 32 If you had any question feel free and ask.
10th Oct 2017, 10:18 AM
Babak
Babak - avatar