关注
test.h
#ifndef _TEST_H_
#define _TEST_H_
typedef void Demo;
typedef void Derived;
Demo* Demo_Create(int i, int j);
int Demo_GetI(Demo* pThis);
int Demo_GetJ(Demo* pThis);
int Demo_Add(Demo* pThis, int value);
void Demo_Free(Demo* pThis);
Derived* Derived_Create(int i, int j, int k);
int Derived_GetK(Derived* pThis);
int Derived_Add(Derived* pThis, int value);
#endif
test.c
#include "test.h"
#include "malloc.h"
static int Demo_Virtual_Add(Demo* pThis, int value);
static int Derived_Virtual_Add(Demo* pThis, int value);
struct VTable // 2. 定义虚函数表数据结构
{
int (*pAdd)(void*, int); // 3. 虚函数表里面存储什么???
};
struct ClassDemo
{
struct VTable* vptr; // 1. 定义虚函数表指针 ==》 虚函数表指针类型???
int mi;
int mj;
};
struct ClassDerived
{
struct ClassDemo d;
int mk;
};
static struct VTable g_Demo_vtbl =
{
Demo_Virtual_Add
};
static struct VTable g_Derived_vtbl =
{
Derived_Virtual_Add
};
Demo* Demo_Create(int i, int j)
{
struct ClassDemo* ret = (struct ClassDemo*)malloc(sizeof(struct ClassDemo));
if( ret != NULL )
{
ret->vptr = &g_Demo_vtbl; // 4. 关联对象和虚函数表
ret->mi = i;
ret->mj = j;
}
return ret;
}
int Demo_GetI(Demo* pThis)
{
struct ClassDemo* obj = (struct ClassDemo*)pThis;
return obj->mi;
}
int Demo_GetJ(Demo* pThis)
{
struct ClassDemo* obj = (struct ClassDemo*)pThis;
return obj->mj;
}
// 6. 定义虚函数表中指针所指向的具体函数
static int Demo_Virtual_Add(Demo* pThis, int value)
{
struct ClassDemo* obj = (struct ClassDemo*)pThis;
return obj->mi + obj->mj + value;
}
// 5. 分析具体的虚函数!!!!
int Demo_Add(Demo* pThis, int value)
{
struct ClassDemo* obj = (struct ClassDemo*)pThis;
return obj->vptr->pAdd(pThis, value);
}
void Demo_Free(Demo* pThis)
{
free(pThis);
}
Derived* Derived_Create(int i, int j, int k)
{
struct ClassDerived* ret = (struct ClassDerived*)malloc(sizeof(struct ClassDerived));
if( ret != NULL )
{
ret->d.vptr = &g_Derived_vtbl;
ret->d.mi = i;
ret->d.mj = j;
ret->mk = k;
}
return ret;
}
int Derived_GetK(Derived* pThis)
{
struct ClassDerived* obj = (struct ClassDerived*)pThis;
return obj->mk;
}
static int Derived_Virtual_Add(Demo* pThis, int value)
{
struct ClassDerived* obj = (struct ClassDerived*)pThis;
return obj->mk + value;
}
int Derived_Add(Derived* pThis, int value)
{
struct ClassDerived* obj = (struct ClassDerived*)pThis;
return obj->d.vptr->pAdd(pThis, value);
}
Main.c
#include "stdio.h"
#include "test.h"
void run(Demo* p, int v)
{
int r = Demo_Add(p, v);
printf("r = %d\n", r);
}
int main()
{
Demo* pb = Demo_Create(1, 2);
Derived* pd = Derived_Create(1, 22, 333);
printf("pb->add(3) = %d\n", Demo_Add(pb, 3));
printf("pd->add(3) = %d\n", Derived_Add(pd,
3));
run(pb, 3);
run(pd, 3);
Demo_Free(pb);
Demo_Free(pd);
return 0;
}
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 26届校招投递进展 #
27901次浏览 217人参与
# 小米提前批笔试难吗 #
33794次浏览 355人参与
# 现代汽车前瞻技术研发急速编程挑战赛 #
8875次浏览 104人参与
# 为了找工作你花了哪些钱? #
26948次浏览 257人参与
# 烟草笔面经互助 #
16786次浏览 180人参与
# 打工人的精神状态 #
49259次浏览 857人参与
# 大疆的机械笔试比去年难吗 #
72803次浏览 618人参与
# 你觉得专业和学校哪个对薪资影响最大 #
61226次浏览 490人参与
# 牛友们,签完三方你在忙什么? #
98113次浏览 852人参与
# 你秋招想去哪些公司 #
21643次浏览 798人参与
# 你今年的保底offer是哪家 #
118142次浏览 537人参与
# 你觉得比亚迪今年还有春招吗? #
191138次浏览 1050人参与
# 视觉/交互/设计百问百答 #
46357次浏览 435人参与
# 秋招结束之后的日子 #
75131次浏览 909人参与
# kpi面有什么特征 #
52168次浏览 402人参与
# 机械人春招想让哪家公司来捞你? #
344413次浏览 3078人参与
# 机械人你觉得今年行情怎么样? #
1285次浏览 27人参与
# 那些我实习了才知道的事 #
210893次浏览 1721人参与
# 如何缓解入职前的焦虑 #
192218次浏览 1339人参与
# 查收我的offer竞争力报告 #
189458次浏览 1265人参与
# 为什么国企只招应届生 #
178303次浏览 1157人参与