[C++基礎]對象內存分佈--虛繼承

單虛擬繼承

class Base
{
public:
	virtual void fun1()
	{
		cout << "Base::func1()" << endl;
	}
	virtual void fun2()
	{
		cout << "Base::func2()" << endl;
	}
private:
	int b;
};
class Derive : virtual public Base
{
public:
	virtual void fun1()
	{
		cout << "Derive::func1()" << endl;
	}
	virtual void fun3()
	{
		cout << "Derive::func3()" << endl;
	}
	void fun4()
	{
		cout << "Derive::func4()" << endl;
	}
private:
	int d;
};
//1 > class Derive	size(20) :
//1 > +-- -
//1 > 0 | {vfptr}
//1 > 4 | {vbptr}
//1 > 8 | d
//1 > +-- -
//1 > +-- - (virtual base Base)
//1 > 12 | {vfptr}
//1 > 16 | b
//1 > +-- -
//1 >
//1 > Derive::$vftable@Derive@:
//1 > | &Derive_meta
//1 > | 0
//1 > 0 | &Derive::fun3
//1 >
//1 > Derive::$vbtable@:
//1 > 0 | -4
//1 > 1 | 8 (Derived(Derive + 4)Base)
//1 >
//1 > Derive::$vftable@Base@:
//1 > | -12
//1 > 0 | &Derive::fun1
//1 > 1 | &Base::fun2
//1 >
//1 > Derive::fun1 this adjustor: 12
//1 > Derive::fun3 this adjustor: 0
//1 > vbi: class  offset o.vbptr  o.vbte fVtorDisp
//1 >       Base      12       4       4 0
//1 >

多虛擬繼承

class Base1 
{
public:
	virtual void fun1()
	{
		cout << "Base1::fun1" << endl;
	}
	virtual void fun2()
	{
		cout << "Base1::fun2" << endl;
	}
private:
	int b1;
};
class Base2
{
public:
	virtual void fun1()
	{
		cout << "Base2::fun1" << endl;
	}
	virtual void fun2()
	{
		cout << "Base2::fun2" << endl;
	}
private:
	int b2;
};
class DeriveA : virtual public Base1, virtual public Base2
{
public:
	virtual void fun1()
	{
		cout << "DeriveA::fun1" << endl;
	}
	virtual void fun3()
	{
		cout << "DeriveA::fun3" << endl;
	}
private:
	int d1;
};
//1 > class DeriveA	size(28) :
//1 > +-- -
//1 > 0 | {vfptr}
//1 > 4 | {vbptr}
//1 > 8 | d1
//1 > +-- -
//1 > +-- - (virtual base Base1)
//1 > 12 | {vfptr}
//1 > 16 | b1
//1 > +-- -
//1 > +-- - (virtual base Base2)
//1 > 20 | {vfptr}
//1 > 24 | b2
//1 > +-- -
//1 >
//1 > DeriveA::$vftable@DeriveA@:
//1 > | &DeriveA_meta
//1 > | 0
//1 > 0 | &DeriveA::fun3
//1 >
//1 > DeriveA::$vbtable@:
//1 > 0 | -4
//1 > 1 | 8 (DeriveAd(DeriveA + 4)Base1)
//1 > 2 | 16 (DeriveAd(DeriveA + 4)Base2)
//1 >
//1 > DeriveA::$vftable@Base1@:
//1 > | -12
//1 > 0 | &DeriveA::fun1
//1 > 1 | &Base1::fun2
//1 >
//1 > DeriveA::$vftable@Base2@:
//1 > | -20
//1 > 0 | &thunk: this -= 8; goto DeriveA::fun1
//1 > 1 | &Base2::fun2
//1 >
//1 > DeriveA::fun1 this adjustor: 12
//1 > DeriveA::fun3 this adjustor: 0
//1 > vbi:	   class  offset o.vbptr  o.vbte fVtorDisp
//1 > Base1      12       4       4 0
//1 > Base2      20       4       8 0
//1 >

菱形虛擬繼承

class Base
{
public:
	virtual void func1()
	{
		cout << "Base::func1()" << endl;
	}
	virtual void func2()
	{
		cout << "Base::func2()" << endl;
	}
private:
	int b;
};
class Base1 : virtual public Base
{
public:
	virtual void func1()
	{
		cout << "Base1::func1()" << endl;
	}
	virtual void func3()
	{
		cout << "Base1::func3()" << endl;
	}
private:
	int b1;
};
class Base2 :virtual public Base
{
public:
	virtual void func1()
	{
		cout << "Base2::func2()" << endl;
	}
	virtual void func4()
	{
		cout << "Base2::func4()" << endl;
	}
private:
	int b2;
};
class Derive : virtual public Base1, virtual public Base2
{
public:
	virtual void func1()
	{
		cout << "Derive::func1()" << endl;
	}
	virtual void func5()
	{
		cout << "Derive::func5()" << endl;
	}
private:
	int d;
};
//1 > class Derive	size(44) :
//1 > +-- -
//1 > 0 | {vfptr}
//1 > 4 | {vbptr}
//1 > 8 | d
//1 > +-- -
//1 > +-- - (virtual base Base)
//1 > 12 | {vfptr}
//1 > 16 | b
//1 > +-- -
//1 > +-- - (virtual base Base1)
//1 > 20 | {vfptr}
//1 > 24 | {vbptr}
//1 > 28 | b1
//1 > +-- -
//1 > +-- - (virtual base Base2)
//1 > 32 | {vfptr}
//1 > 36 | {vbptr}
//1 > 40 | b2
//1 > +-- -
//1 >
//1 > Derive::$vftable@:
//1 > | &Derive_meta
//1 > | 0
//1 > 0 | &Derive::func5
//1 >
//1 > Derive::$vbtable@Derive@:
//1 > 0 | -4
//1 > 1 | 8 (Derived(Derive + 4)Base)
//1 > 2 | 16 (Derived(Derive + 4)Base1)
//1 > 3 | 28 (Derived(Derive + 4)Base2)
//1 >
//1 > Derive::$vftable@Base@:
//1 > | -12
//1 > 0 | &Derive::func1
//1 > 1 | &Base::func2
//1 >
//1 > Derive::$vftable@Base1@:
//1 > | -20
//1 > 0 | &Base1::func3
//1 >
//1 > Derive::$vbtable@Base1@:
//1 > 0 | -4
//1 > 1 | -12 (Derived(Base1 + 4)Base)
//1 >
//1 > Derive::$vftable@Base2@:
//1 > | -32
//1 > 0 | &Base2::func4
//1 >
//1 > Derive::$vbtable@Base2@:
//1 > 0 | -4
//1 > 1 | -24 (Derived(Base2 + 4)Base)
//1 >
//1 > Derive::func1 this adjustor: 12
//1 > Derive::func5 this adjustor: 0
//1 > vbi:	   class  offset o.vbptr  o.vbte fVtorDisp
//1 > Base      12       4       4 0
//1 > Base1      20       4       8 0
//1 > Base2      32       4      12 0
//1 >

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章