在C++中,使用關鍵字virtual聲明爲虛函數。ios
示例C++源碼數組
彙編代碼函數
1.構造函數 this
mov [ebp-8], ecx ;=>保存this指針 mov eax, [ebp-8] ;=>eax得到this指針 mov dword ptr [eax], offset ??_7CVirtual@@6B@ ; const CVirtual::`vftable' ;=>虛表指針初始化
2.析構函數spa
mov [ebp-4], ecx ;=>保存this指針 mov eax, [ebp-4] ;=>eax得到this指針 mov dword ptr [eax], offset ??_7CVirtual@@6B@ ; const CVirtual::`vftable' =>虛表指針重置 push offset aCvirtual ; "~CVirtual!"
3.虛函數調用指針
pVirtual->SetNumber(10); code
push 0Ah ;=>參數10壓棧 mov eax, [ebp-18h] ;=>eax爲this指針 mov edx, [eax] ;=>edx爲虛表指針 mov ecx, [ebp-18h] ;=>ecx傳遞this指針 mov eax, [edx+4] ;=>虛函數SetNumber的地址=虛表+offset 4 call eax