| A function with reference parameters and reference return type. | Lecture 2 - slide 17 : 42 Program 3 |
// Illustrates call by C++ reference parameters.
#include <iostream>
#include <string>
int& f(bool b, int& i, int& j){
if (b)
return i;
else
return j;
}
int main()
{
using namespace std;
int a, b;
a = b = 0;
f(true, a, b) = 7; // assigning to a
cout << "a: " << a << endl; // 7
cout << "b: " << b << endl; // 0
a = b = 0;
f(false, a, b) = 7; // assigning to b
cout << "a: " << a << endl; // 0
cout << "b: " << b << endl; // 7
}