开发中虚函数应用,大大减少开发时间
28 printf("BinNode cleanup\n");
29 BinNode * pBinNode = (BinNode*)pThis;
30 node_cleanup(pBinNode ->_pLeft);
31 node_cleanup(pBinNode ->_pRight);
32 free(pThis);
33 }
34
35 static double _addnode_calc(Node *pThis)
36 {
37 printf("Adding...\n");
38 AddNode * pAddNode = (AddNode*)pThis;
39 return node_calc(pAddNode -> isa._pLeft) + node_calc(pAddNode -> isa._pRight);
40 }
41
42 const NodeVTable _addnode_vtable = { _addnode_calc, _binnode_cleanup };
43
44 static double _multnode_calc(Node *pThis)
45 {
46 printf("Multiplying...\n");
47 MultNode * pMultNode = (MultNode*)pThis;
48 return node_calc(pMultNode -> isa._pLeft)*node_calc(pMultNode -> isa._pRight);
49 }
50
51 const NodeVTable _multnode_vtable = { _multnode_calc, _binnode_cleanup };
52
53 NumNode * newNumNode(double num)
54 {
55 NumNode *pNumNode = malloc(sizeof(NumNode));
56 if(pNumNode != NULL){
57 pNumNode -> isa.vtable = &_numnode_vtable;
58 pNumNode -> _num = num;
59 }
60 return pNumNode; 60 return pNumNode;
61 }
62
63 AddNode * newAddNode(Node *pLeft, Node *pRight)
64 {
65 AddNode *pAddNode = malloc(sizeof(AddNode));
66 if(pAddNode != NULL){
67 pAddNode -> isa.isa.vtable = &_addnode_vtable;
68 pAddNode -> isa._pLeft = pLeft;
69 pAddNode -> isa._pRight = pRight;
70 }
71 return pAddNode;
72 }
73
74 MultNode * newMultNode(Node *pLeft, Node *pRight)
75 {
76 MultNode *pMultNode = malloc(sizeof(MultNode));
77 if(pMultNode != NULL){
78 pMultNode -> isa.isa.vtable = &_multnode_vtable;
79 pMultNode -> isa._pLeft = pLeft;
80 pMultNode -> isa._pRight = pRight;
81 }
82 return pMultNode;
83 }
84
85 double node_calc(Node *pThis)
86 {
87 return pThis -> vtable -> 87 return pThis -> vtable
- 面向对象编程——虚函数(09-20)
- 电源软启动的实用设计技巧(07-16)
- 周立功:动态分布内存——malloc()函数与calloc()函数(07-22)
- 周立功“程序设计与数据结构”:深度解剖动态分布内存的free()函数与realloc()函数(07-25)
- 周立功教你学程序设计技术:做好软件模块的分层设计,回调函数要这样写(07-30)
- 周立功教你学C语言编程:教你数组是如何保存指针的(07-31)