c语言中实践里氏代换原则

任何基类可以出现的地方,都可以用子类来替代。

下面摘录一个单链表的示例:

#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 ", sizeof(struct list)); // struct list* pl 转型为 struct node* pnh struct node* pnh = (struct node*)pl; printf("pnh point to memory sizeof()=%d ", 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 ", sizeof(struct value)); // struct value* pv 也可以转型为 struct node* pnv struct node* pnv = (struct node*)pv; printf("pnv point to memory sizeof()=%d ", sizeof(struct node)); // 把 pnv 接到 pnh 之后 pnh->next = pnv; // 再将 pl->header.next 转型为 pv,可以发现 pl->header.next 就是 pnv printf("pl->header.next->v=%d ", ((struct value*)pl->header.next)->v ); list_destroy(pl); return 0; }

运行结果:

验证:
1、struct list* plist 可以转型为 struct node * pnode 。
2、凡是struct node * pnode 出现的地方,都可以用 struct value * pvalue 来替代。
验证通过!
 
结论:
1、struct node 可以可看作是 struct list 和 struct value 的基类。
2、子类的数据项定义顺序必须把基类的数据项定义放在前面,与基类保持一致。才能正常转型。
3、结构体的空间全部由其数据项大小组合而成,结构体本身不占任何空间。
 
原文地址:https://www.cnblogs.com/godwithus/p/12659427.html