c++ - diff btwn Function returning nonrefrence variable andrefrence variable by reference -
c++ - diff btwn Function returning nonrefrence variable andrefrence variable by reference -
consider below code
int& func1() { int x = 2; homecoming x; } int& func2(int &x) { homecoming x; } int main() { int x = func1(); cout<<"\n : "<<x; int y = 3; x = func2(y); cout<<"\n : "<<x<<"\n"; }
output:
: 2 : 3
the code working absolutely fine , have few doubts have listed below:
in func1() have returned non_reference variable "x" homecoming type reference int, how conversion happening , part of "x" function returning.
in func1() "x" returned reference beingness stack variable how reference of x can returned , mean have no significance after completion ( relating point logic " functions should not homecoming pointers memory allocated in statically").
in func1() returning non refrence variable "x" refrence int , in func2() returning refernce variable "x" refrence int. as both "x" of different type why homecoming type same..
i confused because trying t relate c++ c in clear here majorily due lack of memory layout description of refrence variable, if tell helpful me
in func1
, returning reference something. x
, local func1
, lifetime ends upon returning. then, in main
, assign contents of referred-to variable (x
of func1
, eating dandelions roots) initialize main
's local variable x
. undefined behaviour, means programme allowed interpret wants, formatting hard drive or anything. (most probably, func1
returned pointer variable of called stack frame, still contains right value, because why bother erasing values on stack when crushed next function phone call anyway?) anyway, programme compiled other optimization options, may give answer.
in func2
, returning reference something. referred-to x
, refers main
's y
. then, assign main
's x
value of referred-to variable, y
.
with regards question of memory layout of references, in c, pointers addresses in memory. period. no escape. in c++, references higher-level mechanism "refer something", "talk something", "look it's me right there". they implementable plain pointers. language concept, bit more amenable optimized away, because immutable. (once reference set, cannot changed. , refers -- object @ invalid memory location) that's why standard not specify storage references. freedom of optimization.
update: regards first doubt, main
's x
full-fledged variable (it declared such) , not reference. assigning other value not alter y
's value. in c++, when evaluate reference in expression, so:
int x = 0; int& y = x; // right hand side evaluation of reference y int z = y;
it gets evaluated value of referred-to variable, i.e. value of x
, i.e. 0
. , z
variable distinct x
.
with regards 3rd doubt, func2
homecoming reference. when function declared returning reference, returns reference. reference says "i'm other 1 on there". in case of homecoming value, @ assembler level, provided function not inlined , phone call happens, , on, returned func1
or func2
pointer referred to-variable. actually, in c++, reference int
type of defererencing int
pointer *
. compare previous illustration code same code pointers.
int x = 0; int* const y = &x; int z = *y;
with references, &
, *
occur silently.
just know, references have been introduced language back upwards operator overloading, assignment operator.
// quizz : type of (b = a(123))? if 1mb object? // should type of right-hand side of assignment operator? a, b; = b = a(123);
it can't value or performing horrendously bad (passing result copy). has kind of pointer, can't be. there &
s or *
s somewhere, depending on how word standand functionality. instead of inventing lots of special typesystem cases of operator overloading, stroustrup decided provide 2 orthogonal functionality: references, syntax-fussless immutable pointers (and type of "talking variable"), , operator overloading cleanly enabled references.
c++ reference pass-by-reference
Comments
Post a Comment