关于c++多态,个人认为就是父类调用子类的方法,c++多态的实现主要通过虚函数实现,如果类中含有虚函数,就会出现虚函数表,具体c++多态可以参考《深度探索c++对象模型》
c语言模拟多态主要通过函数指针实现,可以参考《Object Orientated Programming in ANSI-C》
//shape.h #ifndef __SHAPE_H #define __SHAPE_H #include <stdio.h> #include <stdlib.h> typedef struct _functions vFunctions; typedef struct _shape Shape; typedef void (*fptrSet)(void* , int); typedef int (*fptrGet)(void*); typedef void (*fptrDisplay)(); struct _functions{ fptrSet setX; fptrGet getX; fptrSet setY; fptrGet getY; fptrDisplay display; }; struct _shape{ vFunctions functions; int x; int y; }; Shape* getShapesInstances(); void shapeDisplay(Shape *shape); void shapeSetX(Shape *shape, int x); void shapeSetY(Shape *shape, int y); int shapeGetX(Shape *shape); int shapeGetY(Shape *shape); #endif
//Shape.c #include "Shape.h" void shapeDisplay(Shape *shape){ printf("Shape\n"); } void shapeSetX(Shape *shape, int x){ shape->x = x; } void shapeSetY(Shape *shape, int y){ shape->y = y; } int shapeGetX(Shape *shape){ return shape->x; } int shapeGetY(Shape *shape){ return shape->y; } Shape* getShapesInstances(){ Shape *shape = (Shape*)malloc(sizeof(Shape)); shape->functions.display = shapeDisplay; shape->functions.setX = shapeSetX; shape->functions.getX = shapeGetX; shape->functions.setY = shapeSetY; shape->functions.getY = shapeGetY; shape->x = 100; shape->y = 100; return shape; }
//Rectangle.h #ifndef __RECTANGLE__H #define __RECTANGLE__H #include "Shape.h" typedef struct _rectangle{ Shape base; int width; int height; }Rectangle; void rectangleSetX(Rectangle *rectangle, int x); void rectangleSetY(Rectangle *rectangle, int y); int rectangleGetX(Rectangle *rectangle); int rectangleGetY(Rectangle *rectangle); void rectangleDisplay(); Rectangle* getRectangleInstance(); #endif
//Rectange.c #include "Rectange.h" void rectangleSetX(Rectangle *rectangle, int x){ rectangle->base.x = x; } void rectangleSetY(Rectangle *rectangle, int y){ rectangle->base.y = y; } int rectangleGetX(Rectangle *rectangle){ return rectangle->base.x; } int rectangleGetY(Rectangle *rectangle){ return rectangle->base.y; } void rectangleDisplay(){ printf("Rectange\n"); } Rectangle* getRectangleInstance(){ Rectangle *rectangle = (Rectangle *)malloc(sizeof(Rectangle)); rectangle->base.functions.display = rectangleDisplay; rectangle->base.functions.setX = rectangleSetX; rectangle->base.functions.getX = rectangleGetX; rectangle->base.functions.setY = rectangleSetY; rectangle->base.functions.getY = rectangleGetY; rectangle->base.x = 200; rectangle->base.y = 200; rectangle->width = 300; rectangle->height = 500; return rectangle; }
//main.c #include <stdio.h> #include <stdlib.h> #include "Shape.h" #include "Rectange.h" int main() { Shape *sptr = (Shape *)getShapesInstances(); sptr->functions.setX(sptr,35); sptr->functions.display(); sptr->functions.setY(sptr,60); printf("%d\n",sptr->functions.getX(sptr)); Shape *shapes[3]; shapes[0] = getShapesInstances(); shapes[0]->functions.setX(shapes[0],35); shapes[1] = getRectangleInstance(); shapes[1]->functions.setX(shapes[1],45); shapes[2] = getShapesInstances(); shapes[2] ->functions.setX(shapes[2],55); int i =0 ; for( i = 0 ; i < 3; ++ i){ shapes[i]->functions.display(); printf("%d\n",shapes[i]->functions.getX(shapes[i])); } return 0; }
原文:http://www.cnblogs.com/xiongqiangcs/p/3721795.html