有没有办法将一个函数的当前状态传递给 C/C++ 中的另一个函数?我的意思是当前状态下的所有参数和局部变量。例如:
void funcA (int a, int b)
{
char c;
int d, e;
// Do something with the variables.
// ...
funcB();
// Do something more.
}
void funcB()
{
// funcB() should be able to access variables a,b,c,d & e
// and any change in these variables reflect into funcA().
}
如果需要 funcB()
类函数,则代码状态不佳。但是能实现吗?
如果有人开始重构具有多个参数的长方法,这会有所帮助。
最佳答案
引入一个通用的结构。
struct State {
char c;
int d,e;
};
void funcA(int a, int b){
State s;
s.d = 1234; // ...
// ...
funcB(s);
}
void funcB(State& s)
{
//...
}
关于c++ - 将函数的当前状态传递给 C/C++ 中的另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2199969/