C++筆記 第五十一課 C++物件模型分析(下)---狄泰學院
阿新 • • 發佈:2018-12-01
如果在閱讀過程中發現有錯誤,望評論指正,希望大家一起學習,一起進步。
學習C++編譯環境:Linux
第五十一課 C++物件模型分析(下)
1.繼承物件模型
在C++編譯器的內部類可以理解為結構體
子類是由父類成員疊加子類新成員得到的
51-1 繼承物件模型初探
#include <iostream> #include <string> using namespace std; class Demo { protected: int mi; int mj; }; class Derived : public Demo { int mk; public: Derived(int i, int j, int k) { mi = i; mj = j; mk = k; } void print() { cout << "mi = " << mi << "," << "mj = " << mj << "," << "mk = " << mk << endl; } }; struct Test { int mi; int mj; int mk; }; int main() { cout << "sizeof(Demo) = " << sizeof(Demo) << endl; //8 cout << "sizeof(Derived) = " << sizeof(Derived) << endl; //12 Derived d(1,2,3); Test* p = reinterpret_cast<Test*>(&d); cout << "Before changing..." << endl; d.print(); p->mi = 10; p->mj = 20; p->mk = 30; cout << "After changing..." << endl; d.print(); return 0; } 執行結果 sizeof(Demo) = 8 sizeof(Derived) = 12 Before changing... mi = 1,mj = 2,mk = 3 After changing... mi = 10,mj = 20,mk = 30
2.多型物件模型
C++多型的實現原理
當類中宣告虛擬函式時,編譯器會在類中生成一個虛擬函式表
虛擬函式表是一個儲存成員函式地址的資料結構
虛擬函式表是由編譯器自動生成與維護的
virtual成員函式會被編譯器放入虛擬函式表中
存在虛擬函式時,每個物件中都有一個指向虛擬函式表的指標
51-2.c main.c 多型本質分析—C++是通過犧牲效率來實現虛擬函式的
C++中編寫程式:51-2.cpp #include <iostream> #include <string> using namespace std; class Demo { protected: int mi; int mj; public: virtual void print() //宣告為虛擬函式 { cout << "mi = " << mi << "," << "mj = " << mj << endl; } }; class Derived : public Demo { int mk; public: Derived(int i, int j, int k) { mi = i; mj = j; mk = k; } void print() { cout << "mi = " << mi << "," << "mj = " << mj << "," << "mk = " << mk << endl; } }; struct Test { void* p; int mi; int mj; int mk; }; int main() { cout << "sizeof(Demo) = " << sizeof(Demo) << endl; //8 cout << "sizeof(Derived) = " << sizeof(Derived) << endl; //12 Derived d(1,2,3); Test* p = reinterpret_cast<Test*>(&d); cout << "Before changing..." << endl; d.print(); p->mi = 10; p->mj = 20; p->mk = 30; cout << "After changing..." << endl; d.print(); return 0; } 執行結果 sizeof(Demo) = 16 sizeof(Derived) = 24 Before changing... mi = 1,mj = 2,mk = 3 After changing... mi = 10,mj = 20,mk = 30
用C語言實現面向物件的程式碼,來實現多型,也是C++內部的原理
51-2.h #ifndef _51_2_H_ #define _51_2_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
51-2.c
#include "51-2.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);//7.通過物件找到具體的虛擬函式表的指標vptr,通過指標找具體add函式,具體add函式的地址儲存在pAdd成員裡面,找到Demo_Virtual_Add
}
void Demo_Free(Demo* pThis)
{
free(pThis);
}
//8.開始完成子類的部分
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; //10.將vptr關聯到子類的虛擬函式中取
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;
}
//9.在實際的虛擬函式中進行相加操作
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 "51-2.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;
}
執行結果
pb->add(3) = 6
pd->add(3) = 336
r = 6 //父類的呼叫版本
r = 336 //子類的呼叫版本
小結
繼承的本質就是父子間成員變數的疊加
C++中的多型是通過虛擬函式表實現的
虛擬函式表是由編譯器自動生成與維護的
虛擬函式的呼叫效率低於普通成員函式