hap*_*ato 1 c typedef function-declaration
我的程序有几十个(可能超过100个)具有相同参数列表和返回类型的函数.我也可能想为这些函数添加参数.那么,有没有办法用typedef原型(带参数列表)定义这些函数?
例如:我有几十个函数,int f1 (int, int)所以我有几十个声明,如:
int f1 (int x, int y){...}
int f2 (int x, int y){...}
....
int fn(int x, int y){...}
Run Code Online (Sandbox Code Playgroud)
我想定义一些类似于:
typedef int functiontype(int x, int y);
functiontype f1{...}
...
functiontype fn{...}
Run Code Online (Sandbox Code Playgroud)
因此,当我需要升级这些函数时(例如,使用新参数z)我只需要升级typedef语句.这有可能吗?
不,不是真的.使用struct:
typedef struct
{
int x;
int y;
int z; //added
} Params;
int the_function(Params p);
Run Code Online (Sandbox Code Playgroud)
这样你就可以避免破坏声明的函数的源代码z.
使用复合文字,您甚至可以避免命名struct:
the_function((Params){ 2, 5 }); // after adding .z, the source code is unchanged. Its value is 0. Or...
the_function((Params){ .x = 2, .y = 5 }); // named arguments with C99 designated initializers!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |