任何基类可以出现的地方,都可以用子类来替代。
下面摘录一个单链表的示例:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { struct node * next; }; struct list { struct node header;
/***************************************** * 此数据项被映射为 struct node * next; * 占8个字节(64位指针)。因此, * struct list* 可以转型为 struct node* 。 *****************************************/ int len; }; struct value { struct node header; int v; }; struct list* list_create(void) { struct list* pl = (struct list*)malloc(sizeof(struct list)); memset(pl, 0, sizeof(struct list)); pl->header.next = NULL; pl->len = 0; return pl; } void list_destroy(struct list* pl) { free(pl); // memory leak } int main(void) { struct list* pl = list_create(); printf("pl point to memory sizeof()=%d\n", sizeof(struct list)); // struct list* pl 转型为 struct node* pnh struct node* pnh = (struct node*)pl; printf("pnh point to memory sizeof()=%d\n", sizeof(struct node)); struct value* pv = (struct value*)malloc(sizeof(struct value)); memset(pv, 0, sizeof(struct value)); pv->v = 999; printf("pv point to memory sizeof()=%d\n", sizeof(struct value)); // struct value* pv 也可以转型为 struct node* pnv struct node* pnv = (struct node*)pv; printf("pnv point to memory sizeof()=%d\n", sizeof(struct node)); // 把 pnv 接到 pnh 之后 pnh->next = pnv; // 再将 pl->header.next 转型为 pv,可以发现 pl->header.next 就是 pnv printf("pl->header.next->v=%d\n", ((struct value*)pl->header.next)->v ); list_destroy(pl); return 0; }
运行结果:
原文:https://www.cnblogs.com/godwithus/p/12659427.html