Reputation: 21
I have been struggling hard to find the difference between the following two pieces of code.
This ...
int z=10;
int y=&z;
... is invalid whereas the following does not throw any error:
int& foo()
{
int z=10;
return z;
}
main()
{
int y=foo();
cout<<y;
return 0;
}
When I tried to run the program it returned y=10
.
My question is:
If y
can store the reference of another variable using foo()
, why not directly using y=&z
?
Upvotes: 2
Views: 5715
Reputation: 366
The problem is with this code:
int& foo()
{
int z=10;
return z;
}
z is an internal variable whose scope is with-in foo function. As soon as function body ends, z is no more in the memory. But we have passed the reference of internal variable to the outer world (which is out of its scope).
Upvotes: 1
Reputation: 7118
In C++, a reference variable is similar to an alias (as-if having different name for accessing the same variable). As such,
int z=10;
int &y=z; // y is a reference variable, and shares same space of z
y++; // this makes z++ effectively, i.e. z becomes 11
and
int y = &z; // your example is wrong, as rvalue is int * and lvalue is int
Now, when you have,
int& foo()
{
int z=10;
return z;
}
main()
{
int y=foo();
cout<<y;
return 0;
}
This is dangerous, as you are returning reference of a local variable (z) which is going to be destroyed upon returning from foo. This should have been used like returning a value (instead of reference), as local variables go out of scope upon return from function. Thus, correct usage is:
int foo() // return by value
{
int z=10;
return z;
}
main()
{
int y=foo();
cout<<y;
return 0;
}
Hope this helps. Advise is that never ever return reference of a local variable.
Upvotes: 0
Reputation: 902
y
store values, not references, because y
is defined int y
instead of int& y
. Because of this foo( )
returns a reference, but y
stores the value of the reference... that is 10
.
Next code will fail:
int& foo()
{
int z=10;
return z;
}
main()
{
int& y = foo(); // <-- now y is a reference
cout<<y; // <-- at this point z does not exists
return 0;
}
Upvotes: 1
Reputation: 109189
int y=&z;
The ampersand above does not indicate y
is a reference, instead you're applying the address of operator to z
, meaning you're taking its address.
int& y=z;
Here y
is a reference to z
.
In your second example you have undefined behavior because the function foo()
returns a reference to a variable that is local to the function. The storage for z
within foo()
will cease to exist when the function returns, and then accessing it via the returned reference is undefined behavior.
Upvotes: 6
Reputation: 53366
If you want to create reference variable do
int z=10;
int& y=z;
//int y = &z; this is not correct.
Upvotes: 2
Reputation: 3289
When you use the function, you're simply indicating that the function doesn't push the value on the stack, it instead returns the value by reference. Somewhat pointless for a simple int, but more useful if you're returning a struct or class.
In the y=&z
case, you're attempting to set an int equal to an address.
Upvotes: -2
Reputation: 29266
int z=10; // create integer z and set it to the value 10.
int y=&z; // create integer y and set it to the address of z
The code above doesn't have anything to do with references....
int &y = z; // create a reference (y) to integer z
Is more like what you want.
Upvotes: 0