#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
ElemType add(const void *a,const void *b)
{
return *((ElemType *)a)+*((ElemType *)b);
}
void main()
{
void *x1,*x2;
int a=1,b=2;
void *y1="3",*y2="4";
x1=&a;
x2=&b;
printf("%d\n",add(x1,x2));
printf("%d\n",add(y1,y2));
}
以add()函数上函数实现了int类型和char类型的共同借口。
利用void类型来实现c函数重载,在某种意义上可以减少代码量。