这是通过函数指针,实现了调用一个宏定义的方法直接计算不同类型图形的面积的例子。大概形象说明了函数指针的用法:
这应该是重载,不知道多态的话 还有什么更好的例子:
#include#define RECTANGLE 0#define ANGLE 1typedef struct angle_struct{ int t; double a; double b;} angle_struct ;typedef struct rectangle_struct { int t ; double a; double b;}rectangle_struct;double caculate_angle( double a,double b){ return a*b/2;}double caculate_rectangle( double a, double b){ return a*b;}// 函数指针表double (*caculate_tbl[])(double , double ) = { caculate_rectangle , caculate_angle };#define caculate_area(z) caculate_tbl[z.t](z.a, z.b)int main(){ angle_struct angle; angle.t = ANGLE; angle.a=1; angle.b=2; rectangle_struct rectangle; rectangle.t = RECTANGLE; rectangle.a = 1; rectangle.b = 2; printf("the area of rectangle is %f and angle is %f \n", caculate_area(rectangle), caculate_area(angle)); return 1;}