references

can anyone plz explain me the output of the following code
Also the output when static keyword is removed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
 
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    getchar();
    return 0;
}
Last edited on
Function fun returns a reference to an object with the static storage duration.

After the statement

fun() = 30;

variable x defined in the body of func will be equal to 30.
Statement

cout << fun();

outputs this value on the console.
Topic archived. No new replies allowed.