returning references

if i omit the static keyword,the output changes,but y?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
 
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}
Read this:
http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/

In essence, the static keyword associated with 'x' preserves the address of the integer for the duration of the program. Where as without the static keyword, the address becomes invalidated once the scope of the function has ended. i.e. after line 8.
Topic archived. No new replies allowed.