如何在C语言中实现功能重载?在C中是否有实现函数重载的方法?我正在查看要重载的简单函数,如foo (int a) foo (char b) foo (float c , int d)我认为没有直截了当的办法,我正在寻找解决办法,如果存在的话。
3 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
struct
struct
enum
union
#include <stdio.h>typedef enum { T_INT, T_FLOAT, T_CHAR,} my_type;typedef struct { my_type type; union { int a; float b; char c; } my_union;} my_struct;void set_overload (my_struct *whatever) { switch (whatever->type) { case T_INT: whatever->my_union.a = 1; break; case T_FLOAT: whatever->my_union.b = 2.0; break; case T_CHAR: whatever->my_union.c = '3'; }}void printf_overload (my_struct *whatever) { switch (whatever->type) { case T_INT: printf("%d\n", whatever->my_union.a); break; case T_FLOAT: printf("%f\n", whatever->my_union.b); break; case T_CHAR: printf("%c\n", whatever->my_union.c); break; }}int main (int argc, char* argv[]){ my_struct s; s.type=T_INT; set_overload(&s); printf_overload(&s); s.type=T_FLOAT; set_overload(&s); printf_overload(&s); s.type=T_CHAR; set_overload(&s); printf_overload(&s); }
- 3 回答
- 0 关注
- 950 浏览
添加回答
举报
0/150
提交
取消