在 C/C++ 中将一个函数的当前状态传递给另一个函数
在 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()
类型的函数,则代码的形状很糟糕。但能实现吗?
如果有人开始重构具有多个参数的长方法,这会有所帮助。
Is there a way to pass the current state of a function into another function in C/C++? I mean all the parameters and local variables by current state. For example:
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().
}
The code is in bad shape if there is a need for funcB()
kind of functions. But can it be achieved?
This can help if someone is starting to re-factor a long method with multiple parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
引入一个通用的结构体。
Introduce a common struct.
gcc 支持类似 Pascal 的 C 扩展,即嵌套函数,例如
使用 --nested-functions 编译它:
然后运行它:
当然这是不可移植的,但如果您只使用 gcc 并且您有太多遗留代码重写的话这可能是一个很好的解决方案。
gcc supports a Pascal-like extension to C, namely nested functions, e.g.
Compile this with --nested-functions:
and then run it:
Of course this is non-portable but if you are only using gcc and you have too much legacy code to re-write then this might be a good solution.
另一种可能比第一种更优雅。它使用带有静态成员的结构。
用法
编辑:使成员不是静态的。这样更好。
Another one that may be more elegant than the first. It uses a struct with static members.
Usage
EDITED: Made members not static. Better that way.
创建一个结构来保存您关心的所有状态。
根据结构体编写这两个函数;也就是说,它们将结构体的实例作为唯一参数,并且所有状态都保存在结构体中。
然后将结构实例从 a 传递到 b。更有效的方法是将指针传递给结构体,但传递结构体也可以。
Create a struct to hold all the state you care about.
Write both functions in in terms of the struct; that is, they take as their only parameter an instance of the struct, and all state is saved in the struct.
Then pass the struct instance from a to b. More efficient would be to pass a pointer to the struct, but passing the struct will work.
不,没有,原则上数据都是可用的,它们都在堆栈上,但是 C 没有提供访问它们的机制,除非您开始使用内联汇编和指针等。不是你想做的事。最好只传递你需要的东西。
No there isn't, in principle the data is all available, it's all on the stack, but C provides no mechanism to access them, unless you start mucking about with inline assembly and pointers and the like. Not something you want to be doing. Better to just pass what you need.
为了解决这个特定问题,您可以使用对象并在构造函数中将一些常用参数绑定到对象的成员。然后让你的函数(接受这些长参数列表)成为成员函数。
To beat this particular problem, you could use objects and just bind in constructor some of common used parameters to object's members. And than make your functions (that accept these long lists of parameters) member-functions.
最好的解决方案是将变量放入结构中,并通过引用其他函数来传递该结构。
不过,还有另一种解决方案可以使用宏。宏可以访问同一范围内的任何变量,但我不推荐这种方法。
The best solution is to put variables in a structure and pass this structure by reference to other function.
Though, there is another solution you can use macros. Macros are able access any variable you have in the same scope, but I'm not recommending this approach.
这样做会破坏模块化和创建函数的整体目的。 funcB 只能由某些其他函数调用,特别是那些定义它所依赖的变量的函数。对于编写调用
funcB
的新代码的人来说,这一点根本不清楚。正如其他人所建议的,我要么为此目的创建一个结构,要么通过引用传递所有需要的变量,以明确
funcB
所依赖的内容。Doing something like this would break modularity and the whole purpose of creating functions.
funcB
would only be callable by certain other functions, specifically those that define the variables it depends on. This would not be at all clear to someone writing new code callingfuncB
.As others have suggested, I would either create a struct just for this purpose or pass all the needed variables by reference to make it clear what
funcB
relies on.您可能想要做的是:将函数封装到一个类中并创建它的单例实例。从技术上讲,您希望在这些函数之间共享的状态是 OOP 中通过实例化对象并调用它们的方法而发生的情况。
因此,如果 C++ 是一种选择(因为 C 没有 OOP),我建议采用该解决方案。
另一种方法是使用 lambda/闭包,但这在 C/C++ 中不是一个选项,因为它不支持这些语言构造。
编辑:如果您知道如何在整个代码中正确处理这些单例,则仅使用单例...
What you probably want to do, is: encapsulate your functions into a class and create a singleton instance of it. The state you want to share between those functions is what technically happens in OOP by instantiating objects and invoke methods on them.
So if C++ is an option (as C has no OOP) I would recommend going with that solution.
Another approach would be to use lambdas/closures but that is not an option in C/C++ because it does not support those language constructs.
Edit: Only use singletons if you know how to handle these correctly throughout your code...