Tuesday, November 23, 2010

Function Returning Lvalue

The following program compiles and prints:
1
2

#include <iostream>
using namespace std;

int & func() {
  static int temp = 1;
  cout << temp << endl;
  return temp;
}

int main() {
  func() = 2;
  func();
  return 0;
}

No comments:

Post a Comment