00 // Annother illustration of C++ reference parameters. 
01 // Attempting to return a reference to a local variable. Compiles with warnings.
02 
03 #include <iostream>
04 #include <string>
05 
06 int& f(bool b)
07 {
08   int i = 5, j = 10;
09 
10   std::cout << "i: " << i << "  ";
11   std::cout << "j: " << j << std::endl;
12 
13   if (b)
14     return i;     // error (warning) Reference to local variable returned.
15   else 
16     return j;     // error (warning): Reference to local variable returned.
17 }
18 
19 // A diferrent version
20 int& f(bool b){
21   static int i = 5, j = 10;
22 
23   std::cout << "i: " << i << "  ";
24   std::cout << "j: " << j << std::endl;
25 
26   if (b)
27     return i;     
28   else 
29     return j;     
30 }
31 
32 
33 int main()
34 {
35 
36   int a, b;
37   a = b = 0;
38 
39   f(true) = 15;          
40   f(false) = 20;         
41   f(true) = 25;          
42 }
43 
44 
45