怎样用C语言实现 函数重载
怎样用C语言实现 函数重载,用宏应该可以实现,但是有没有大牛给个例子, 其它方法实现也可以。 谢谢。。。
[解决办法]
#include <stdio.h>double A[300][100];double valuex[300];double valuey[300];int i;double fun00(double x,double y) {return x + y ;};double fun01(double x,double y) {return x*x+ 3*y ;};double fun02(double x,double y) {return x + 2*y*x;};//... fun03(double x,double y) {return ...+... ;};//...//... fun98(double x,double y) {return ...+... ;};double fun99(double x,double y) {return x/2+20*y ;};double (*funNN[100])(double,double)={ fun00, fun01, fun02,// fun03,// ...// fun98, fun99,};int main() { double x,y; int f,FN; for (i=0;i<300;i++) { valuex[i]=(double)i; valuey[i]=(double)i; } FN=3; for (i=0;i<300;i++) { x=valuex[i]; y=valuey[i]; for (f=0;f<FN;f++) A[i][f]=funNN[f](x,y); } for (i=0;i<3;i++) { for (f=0;f<FN;f++) printf("%lg ",A[i][f]); printf("\n"); } return 0;}//0 0 0//2 4 3//4 10 10
[解决办法]
#define FUNC(type, data) {type d1 = data; func(#type, &d1);}void func1(int i) {printf("this is function of int type, value = %d\n\n", i);}void func2(float f) {printf("this is function of float type, value = %f\n\n", f);}void func(char* sz, void* p){ if (strcmp(sz, "int") == 0) { func1(*(int*)p); } if (strcmp(sz, "float") == 0) { func2(*(float*)p); }}int main(){ FUNC(int, 8); FUNC(float, 8); return 0;}