?麯???C++?????
???????????? ???????[ 2015/1/21 13:30:35 ] ????????C++ net
?????麯??
????C++?е??麯????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????“???????”???????????????????ν?????????????????????ò??????????????????????磺??弼????RTTI???????麯????????????????????????????飬?????????????????顣
?????????麯???
????????????? ?????????????????virtual????? ?????????????????????????????????????????????????.??????麯????
?????????麯?????????
????1 ????麯??
????class Test
????{
????public:
????virtual add(int??int); //??????麯??
????}
????2 ???麯??
????class Test
????{
????public:
????virtual add(int??int)=0 //???麯??
????}
??????????麯??: ???????????к???????????????????????????????????????
???????麯??:????????????к???????????????????????????????????????0?????д??麯?????????????????????????????????????????????????????????
?????麯?????????
?????????ó??????????????????????????????????????????? ???????????????????????????????????????????????????????????ж?????????????????????д??ó??????????
??????????麯?????????????????????????????????????
?????????????ж?????????????
?????????麯?????
??????C++ ??????????????麯????Virtual Function???????????麯?????Virtual Table?????????????V-Table??????????У?????????????麯???????????????????С???????????????????????????????????????????麯??????????????????????????????????????У???????????????????????????????????????????麯?????????????????????????????????????????????????????
?????????????????????????麯?????C++???????????????麯?????????????????????????λ????????????????麯??????и??????——????ж???л???????е?????£???????ζ????????????????????????????麯?????????????????к??????????????????????
?????????????????????о?????????????????????????????????????????????????????????????????????????
?????????????????????????
????classBase {
????public:
????virtualvoid f() { cout << "Base::f"<< endl; }
????virtualvoid g() { cout << "Base::g"<< endl; }
????virtualvoid h() { cout << "Base::h"<< endl; }
????};
????????????????????????????Base???????????麯???????????????????
????typedefvoid(*Fun)(void);
????Base b;
????Fun pFun = NULL;
????cout << "?麯????????" << (int*)(&b) << endl;
????cout << "?麯???? — ??????????????" << (int*)*(int*)(&b) << endl;
????// Invoke the first virtual function
????pFun = (Fun)*((int*)*(int*)(&b));
????pFun();
??????????о??????£?(WindowsXP+VS2003?? Linux 2.6.22 + GCC 4.1.3)
?????麯????????0012FED4
?????麯???? — ??????????????0044F148
????Base::f
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11