關於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; }