{"id":354,"date":"2019-09-10T17:03:54","date_gmt":"2019-09-10T09:03:54","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=354"},"modified":"2020-09-27T11:23:16","modified_gmt":"2020-09-27T03:23:16","slug":"vtable%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=354","title":{"rendered":"[C++] VTable\u6d4b\u8bd5"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2019\/09\/VTable.gif\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code> 1 typedef unsigned int uint;\n 2 typedef void (*callPtr)();\n 3 \n 4 class CBaseClass\n 5 {\n 6 public:\n 7     CBaseClass(){}\n 8     ~CBaseClass(){}\n 9     virtual void Print()\n10     {\n11         printf(\"Call: CBaseClass Print\\n\");\n12     }\n13 };\n14 \n15 class CDeriveClass: public CBaseClass\n16 {\n17 public:\n18     CDeriveClass(){}\n19     ~CDeriveClass(){}\n20     virtual void Print()\n21     {\n22         printf(\"Call: CDeriveClass Print\\n\");\n23     }\n24 };\n25 \n26 int main()\n27 {\n28     \/\/ \u521b\u5efa\u5bf9\u8c61 pDeriveA\n29     CBaseClass* pDeriveA= new CDeriveClass();\n30     \/\/ \u83b7\u5f97\u5bf9\u8c61A\u7684vtable\n31     uint* pVTableA= (uint*)*((uint*)pDeriveA);\n32     \/\/ \u53d6\u5f97vtable\u4e2d\u7b2c0\u4e2afunction\n33     callPtr funcA= (callPtr)pVTableA[0];\n34     \/\/ \u8c03\u7528Print\n35     funcA();\n36 \n37     \/\/ \u521b\u5efa\u5bf9\u8c61 pDeriveB\n38     CBaseClass* pDeriveB= new CDeriveClass();\n39     \/\/ \u83b7\u5f97\u5bf9\u8c61B\u7684vtable\n40     uint* pVTableB= (uint*)*((uint*)pDeriveB);\n41     \/\/ 2\u4e2a\u5bf9\u8c61\u7684VTable\u5e94\u8be5\u76f8\u7b49\n42     printf(\"vtable %s\\n\", (pVTableA== pVTableB) ? \"Equal!\" : \"Not Equal!\");\n43 \n44     return 0;\n45 }\n46<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[20],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/354"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=354"}],"version-history":[{"count":3,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":574,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions\/574"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}