In C++ there is no way to access locally declared function variables outside of that function's scope. Simply put, what you are asking for here:
I must access variables declared inside another function.
is simply impossible. Anything you try that seems to allow you to do that is undefined behavior.
What you can do is turn "f1" and "f2" as methods of a class and put double a
and int b
as member data states:
class c1
{
double a;
int b;
public:
void f1();
void f2();
};
void c1::f1()
{
// f1 can access a and b.
//some operations
}
void c1::f2()
{
// f2 can see the changes made to a and b by f1
}
This fulfills your two requirements. Namely:
- No global variables are used.
- No parameter references are passed into the methods in question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…