#include<iostream>
usingnamespace std;
/* such a function will not be safe if x is non static variable of it */
int &fun()
{
staticint x;
return x;
}
int main()
{
fun() = 10;
/* this line prints 10 on screen */
printf(" %d ", fun());
getchar();
return 0;
}
o/p=10
how it is working i m not getting it and why this function is not safe if not static..can anyone explain plzz.
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.
static guarantees that the variable's lifetime is from when the function is first called to the end of the program - it's effectively a global variable. Without it, the function would be trying to return a reference to a variable that stops existing when the function ends.
Here's the basic concept. fun returns a reference to an integer. Because it's a reference, it behaves as if the actual integer is there when func() returns.
You asked an important question, but you formed the question incorrectly. What you asked was why the FUNCTION was unsafe if not static. You probably intended to communicate an inquiry about x being static, but your wording accidentally asked about an important distinction. The function is static because it's not a member function of a class, but it could be a member of a class. If it were, the function can be declared static, but for the question about the return value failing when non-static, the issue does not apply to whether the function is declared static, but a question about what is returned.
In the case of the function, the value being returned is a reference, as I pointed out. If x were not declared static, just int x, that would be a local variable. Local variables are deleted upon return. They evaporate. The reference returns the actual x, not a copy of x, by reference. This means that if the value is destroyed when the function returns, you would be returning an object that has just disappeared.
The key is not about the declaration of x being static, but that x must be fashioned to survive beyond the return of the function.
That requirement can be satisfied a number of ways. In this case, declaring x static works, because it defines x as static storage - meaning it exists from the moment the program begins executing until the program exits. It basically makes the storage global.
That means the same effect is achieved if x had been declared outside the function (that is, x is in global scope).
The requirement that x survives the return of the function can be satisfied if x were the member of a class, AND the function were also a member function that class.
You will need to better understand what references are to really "get" this.
you need to understand the concept of scope duration.
if the variable x was not static it will be an automatic variable (local storage of the function) then when exiting the function , it will get destroyed by the stack. So you will receive an invalid reference. Static keyword can most of time be avoid , but sometime is necessary for specific usage . You should know that also global scope is to be avoided too .
In your code , there is no need to create a function , simply put int x = 10 ; in the main function.
reference in c++ ...is a const pointer . (int &j=i) where j is reference to referand i.
so if i=10;
int &j=i;
so value of *j=10.
in main first the function is called and then value is assigned 10 to fun() which is treated as integer now ? its like very little but i got confused...i was thinking 10 is assigned first ..i think it has to do with precedence :)
reference in c++ ...is a const pointer . (int &j=i) where j is reference to referand i.
A reference in C++ is.. a reference. You can think of it as another name (or alias) for a variable.
imii wrote:
so if i=10;int &j=i;
so value of *j=10.
The expression *j is nonsensical. j is not a pointer and it cannot be dereferenced. A C++ compiler would flag this as a syntax error. The value of j is i, which is 10.