首页 > 解决方案 > 将参数分组到结构中

问题描述

我有一个带有大量输出参数的函数,目前看起来像

int do_some_foo(char **string_out, int *index_out, char **description_out, etc...);

因此,由于 out 参数可能会增长并且目前总共已经大约 8 个(其中一些为简洁起见省略了),我倾向于将所有参数分组到一个结构中并记录调用者不拥有 out 指针

struct foo_out{
    int status;
    char *string;
    int index;
    char *description;
    //...
};

//Pointers contained in the return value are not owned by
//the caller and should not be modified or reclaimed by it in 
//any way
const struct foo_out *do_some_foo(int *error_code);

//Release all the resources used by the library
//all const struct foo_out * pointers returned by do_some_foo
//becomes invalid
void prepare_shutdown(void);

通过这种方式,我想我在释放库使用的资源后限制了被调用者使用这个指针。所以调用者在调用它之后不应该使用指针。

标签: cout-parameters

解决方案


推荐阅读