请教下一个小问题,C++派生类都会包含基类对象成员吗?

详细补充下:无论派生类是已何种方式继承基类,包括public protected private,基类对象成员也可以是public protected private,我想问无论派生类以哪种方式继承基类,是不是在内存中派生类都会含有基类对象成员,哪怕派生类无法访问?
今天突然想到的一个小问题,求大神告知。
全部评论
在C++编译器的内部 类可以理解为结构体 子类是由父类成员叠加子类新成员得到的 C++多态实现原理:
点赞
送花
回复
分享
发布于 2017-06-22 07:49
是的...
点赞
送花
回复
分享
发布于 2017-06-21 23:35
滴滴
校招火热招聘中
官网直投
应该吧,举个例子。父类指针指向子类对象,父类指针可以访问private继承的子类对象中属于父类部分的成员,所以内存应该存在
点赞
送花
回复
分享
发布于 2017-06-22 00:26
存在,最直接的例子就是多态机制中的虚函数表,可以百度下陈皓大神的那个blog,那个经典啊,有图有理,直观明了
点赞
送花
回复
分享
发布于 2017-06-22 07:10
会包含。我给你一个C实现c++多态和继承的例子
点赞
送花
回复
分享
发布于 2017-06-22 07:37
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; }
点赞
送花
回复
分享
发布于 2017-06-22 07:42
感谢各位大佬的回复。我在C++ primer 5th p534里有看到这么一句话“一个基类的对象可能是派生类对象的一部分,也可能不是”,请问下这句话是什么意思呢?我没有搞太懂,是不是和前面所表述的意思不一样?
点赞
送花
回复
分享
发布于 2017-06-22 09:49
写的太概念性了吧,愿意应该是,基类的对象可以独立存在,独立存在的话,那就不是派生类对象的一部分了---这就是那个可能不是。
点赞
送花
回复
分享
发布于 2017-06-24 19:46
我去翻了下,根据上下文理解。就是这个意思
点赞
送花
回复
分享
发布于 2017-06-24 19:52
然后书上举了两个 指针转换的例子。你动手写一写
点赞
送花
回复
分享
发布于 2017-06-24 19:54

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务