Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          references/ref1.cc - A function with reference parameters and reference return type.Lecture 2 - slide 15 : 29
Program 6

// Annother illustration of C++ reference parameters. 
// A rather strange example involving a function that returns a reference.

#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
}