freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內容

團隊學生技術交流講座(xxxx)_第2課_理解c虛擬多態(tài)機制(編輯修改稿)

2025-03-15 12:18 本頁面
 

【文章內容簡介】 用的函數(shù)。 ? 動態(tài)綁定 (dynamic/later binding) – 綁定過程工作在程序 運行 時執(zhí)行,在程序運行時才確定將要調用的函數(shù)。 “神奇的” 執(zhí)行方式 —— 動態(tài)綁定 ? C++的 虛函數(shù) 在運行時正是通過 動態(tài)綁定 實現(xiàn)多態(tài)機制的。 ? 動態(tài)綁定是如何實現(xiàn)的呢? ? 動態(tài)綁定是通過編譯器運用 虛函數(shù)表 VTBL和 虛表指針 VPTR而實現(xiàn),在運行時通過動態(tài)查詢實現(xiàn)對正確函數(shù)體的綁定。 VTBL – 編譯器會為聲明了虛函數(shù)的類建立一個虛函數(shù)表。 – VTBL實際上是一個 函數(shù)指針數(shù)組 ,每個虛函數(shù)占用這個數(shù)組的一個入口( slot)。 – 一個類只有一個 VTBL,不管它有多少個實例。 – 派生類有自己的 VTBL,但是派生類的 VTBL與基類的 VTBL有相同的函數(shù)排列順序,同名的虛函數(shù)被放在兩個數(shù)組的相同位置上。 VPTR ? 在創(chuàng)建類實例的時候,編譯器還會在每個實例的內存布局中增加一個 VPTR指針,該指針指向本類的 VTBL。 最后,是動態(tài)聯(lián)編 ? 在調用此類的構造函數(shù)時 , 在類的構造函數(shù)中 ,編譯器會隱含執(zhí)行 VPTR與 VTBL的關聯(lián)代碼 ,將 VPTR指向對應的 VTBL。 這就將類與此類的 VTBL聯(lián)系了起來 。 ? 在調用類的構造函數(shù)時 , 指向基類的指針此時已經(jīng)變成指向具體的類的 this指針 , 依靠此 this指針 ,進一步得到正確的 VPTR, 即可得到正確的 VTBL, 從而實現(xiàn)了多態(tài)性 。 在此時才能真正與函數(shù)體進行連接 , 這就是動態(tài)聯(lián)編 。 一個更大的例子 … ? include iostream ? using namespace std。 ? enum note { middleC, Csharp, Cflat }。 // Etc. ? class Instrument ? { ? public: ? virtual void play(note) const ? { cout Instrument::play endl。 } ? virtual char* what() const ? { return Instrument。 } ? virtual void adjust(int) {} ? }。 ? class Wind : public Instrument ? { ? public: ? void play(note) const ? { cout Wind::play endl。 } ? char* what() const { return Wind。 } ? void adjust(int) {} ? }。 ? class Percussion : public Instrument ? { ? public: ? void play(note) const ? { cout Percussion::play endl。 } ? char* what() const { return Percussion。 } ? void adjust(int) {} ? }。 ? class Stringed : public Instrument ? { ? public: ? void play(note) const ? { cout Stringed::play endl。 } ? char* what() const ? { return Stringed。 } ? void adjust(int) {} ? }。 一個更大的例子 … ? class Brass : public Wind ? { ? public: ? void play(note) const ? { cout Brass::play endl。 } ? char* what() const ? { return Brass。 } ? }。 ? class Woodwind : public Wind ? { ? public: ? void play(note) const ? { cout Woodwind::play endl。 } ? char* what() const ? { return Woodwind。 } ? }。 ? void tune(Instrument i) ? { ? (middleC)。 ? } ? void f(Instrument i) ? { ? (1)。 ? } ? // Upcasting during array initialization: ? Instrument* A[] = ? { ? new Wind, ? new Percussion, ? new Stringe
點擊復制文檔內容
教學課件相關推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1