在 C/C++ 中将一个函数的当前状态传递给另一个函数

发布于 2024-08-20 10:04:31 字数 475 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

云淡月浅 2024-08-27 10:04:31

引入一个通用的结构体。

struct State {
    char c;
    int d,e;
};

void funcA(int a, int b){
    State s;
    s.d = 1234; // ...
    // ...
    funcB(s);
}

void funcB(State& s)
{
    //...
}

Introduce a common struct.

struct State {
    char c;
    int d,e;
};

void funcA(int a, int b){
    State s;
    s.d = 1234; // ...
    // ...
    funcB(s);
}

void funcB(State& s)
{
    //...
}
余生共白头 2024-08-27 10:04:31

gcc 支持类似 Pascal 的 C 扩展,即嵌套函数,例如

#include <stdio.h>

int main(void)
{
    int a = 1, b = 2;

    void foo(void)
    {
        printf("%s: a = %d, b = %d\n", __FUNCTION__, a, b);
    }

    printf("%s: a = %d, b = %d\n", __FUNCTION__, a, b);

    foo();

    return 0;
}

使用 --nested-functions 编译它:

$ gcc -Wall --nested-functions nested.c -o nested

然后运行它:

$./nested
main: a = 1, b = 2
foo: a = 1, b = 2

当然这是不可移植的,但如果您只使用 gcc 并且您有太多遗留代码重写的话这可能是一个很好的解决方案。

gcc supports a Pascal-like extension to C, namely nested functions, e.g.

#include <stdio.h>

int main(void)
{
    int a = 1, b = 2;

    void foo(void)
    {
        printf("%s: a = %d, b = %d\n", __FUNCTION__, a, b);
    }

    printf("%s: a = %d, b = %d\n", __FUNCTION__, a, b);

    foo();

    return 0;
}

Compile this with --nested-functions:

$ gcc -Wall --nested-functions nested.c -o nested

and then run it:

$./nested
main: a = 1, b = 2
foo: a = 1, b = 2

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.

你如我软肋 2024-08-27 10:04:31

另一种可能比第一种更优雅。它使用带有静态成员的结构。

struct SomeAlgo {
    SomeAlgo()
    : c(0), d(0), e(0) // Initialize common variables
    { }
    void funcA(int a,int b)
    {
        c = 1234; //...
        // ...
        funcB();

    }
    void funcB() // You may put it in the private section.
    {
        // Simply use c,d,e here.
    }
private:
    char c;
    int d,e;
};

用法

SomeAlgo alg;
alg.funcA(3,4);

编辑:使成员不是静态的。这样更好。

Another one that may be more elegant than the first. It uses a struct with static members.

struct SomeAlgo {
    SomeAlgo()
    : c(0), d(0), e(0) // Initialize common variables
    { }
    void funcA(int a,int b)
    {
        c = 1234; //...
        // ...
        funcB();

    }
    void funcB() // You may put it in the private section.
    {
        // Simply use c,d,e here.
    }
private:
    char c;
    int d,e;
};

Usage

SomeAlgo alg;
alg.funcA(3,4);

EDITED: Made members not static. Better that way.

女皇必胜 2024-08-27 10:04:31

创建一个结构来保存您关心的所有状态。

根据结构体编写这两个函数;也就是说,它们将结构体的实例作为唯一参数,并且所有状态都保存在结构体中。

struct foo {  int d; int e; char c ; } fooinstance;

void afunc( struct foo fi);
void bfunc( struct foo fi);

void afunc( struct foo fi ) {
  // do stuff with fi
  f1.d += fi.e ;
  fi.c++;
  // call b
  b(fi);
}

然后将结构实例从 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.

struct foo {  int d; int e; char c ; } fooinstance;

void afunc( struct foo fi);
void bfunc( struct foo fi);

void afunc( struct foo fi ) {
  // do stuff with fi
  f1.d += fi.e ;
  fi.c++;
  // call b
  b(fi);
}

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.

蹲在坟头点根烟 2024-08-27 10:04:31

不,没有,原则上数据都是可用的,它们都在堆栈上,但是 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.

护你周全 2024-08-27 10:04:31

如果有人开始,这会有所帮助
重构一个长方法
多个参数。

为了解决这个特定问题,您可以使用对象并在构造函数中将一些常用参数绑定到对象的成员。然后让你的函数(接受这些长参数列表)成为成员函数。

This can help if someone is starting
to re-factor a long method with
multiple parameters.

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.

森林散布 2024-08-27 10:04:31

最好的解决方案是将变量放入结构中,并通过引用其他函数来传递该结构。
不过,还有另一种解决方案可以使用宏。宏可以访问同一范围内的任何变量,但我不推荐这种方法。

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.

弃爱 2024-08-27 10:04:31

这样做会破坏模块化和创建函数的整体目的。 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 calling funcB.

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.

烏雲後面有陽光 2024-08-27 10:04:31

您可能想要做的是:将函数封装到一个类中并创建它的单例实例。从技术上讲,您希望在这些函数之间共享的状态是 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文