摘要: 下面来看看虚基类对对象内存布局的影响。虚基类的主要作用就是在所有的派生类中,保留且仅保留一份虚基类的suboject。a. 一个虚基类的情况#include using namespace std;class Base{public: int base_member;};class Derived : publicvirtualBase {};int main(void){ Base b; Derived d; cout using namespace std;class Base1{public: int base1_member;};class Base2{public: int base 阅读全文
posted @ 2013-10-27 22:08 0弓虽 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 假定多层继承的各类之间的关系如下图。假定派生类不override基类的虚函数,即Base2不override Base1中声明的虚函数vfBase1(),Base3不override Base2中声明的虚函数vfBase2(),Derived不override Base3中声明的虚函数vfBase3()。 1 #include 2 3 using namespace std; 4 5 6 7 class Base1 8 9 {10 11 public:12 13 int m_base1;14 15 16 17 inline vi... 阅读全文
posted @ 2013-10-27 22:05 0弓虽 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 注意:关于内存对齐(memory alignment),请看关于内存对齐问题,后面将会用到。下面我们进行在普通继承(即非虚继承)时,派生类的指针转换到基类指针的情形研究。假定各类之间的关系如下图:代码如下:#include using namespace std;#pragma vtordisp(off)class Parent{public: int parent;};class Child : public Parent{public: int child;};class GrandChild : public Child{public: int grandchild;};int main 阅读全文
posted @ 2013-10-27 22:03 0弓虽 阅读(192) 评论(0) 推荐(0) 编辑
摘要: 转载http://blog.csdn.net/pathuang68/article/details/41019815内容概要:满足下面2个条件时,1. 父类有虚函数,子类也有虚函数,且子类的虚函数重写或覆盖了父类的虚函数2. 非虚继承类对象之内存布局在前面的例子中,恢复原来的两个虚函数vfBase_1()和vfBase_2(),同时在Derived类中重写基类的虚函数vfBase_1(),Base类和Derived类之间的关系如下图: 1 #include 2 3 using namespace std; 4 5 6 7 class Base 8 9 {10 11 public:... 阅读全文
posted @ 2013-10-27 22:00 0弓虽 阅读(240) 评论(0) 推荐(0) 编辑
摘要: 转载来自http://blog.csdn.net/pathuang68/article/details/41019701内容概要:满足下面2个条件时,1. 父类有虚函数,子类无虚函数(即无虚函数重写或无虚函数覆盖)2. 非虚继承类对象之内存布局Base类中有两个虚函数vfBase_1()、vfBase_2()和一个整形成员变量m_base, Derived类中有一个整形成员变量m_derived,二者的关系如下: 1 #include 2 using namespace std; 3 class Base 4 { 5 public: 6 int m_base; 7 ... 阅读全文
posted @ 2013-10-27 21:56 0弓虽 阅读(185) 评论(0) 推荐(0) 编辑