I just go through an interview where i was asked how can i access local variable of function to other function. code flow-
class A{
public:
void fun1(int a){
int local_var=a;
}
void fun2(){
// here i want to use local_var of fun1
//you are not allowed to do any changes either in fun1 and class
// however you can do anythingh in fun2.
}
}
is there any way to do it.
local_vargoes out of scope the momentfun1returns and you're not allowed to modify the function, clearly, you can't read the value in any other scope.intstack variable tofun2without ever initializing it, and callingfun2immediately afterfun1, may end up getting you the original value oflocal_varas the "trash" value.