{"id":12144,"date":"2021-09-08T10:04:12","date_gmt":"2021-09-08T10:04:12","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=12144"},"modified":"2024-02-16T11:10:28","modified_gmt":"2024-02-16T11:10:28","slug":"vtable-a-way-to-realise-polymorphism-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=12144","title":{"rendered":"vtable &#8211; a way to realise polymorphism (C++)"},"content":{"rendered":"<p>Currently most C++ compilers use vtables to realise polymorphism.<\/p>\n<pre class=\"brush: cpp; title: main.cpp; notranslate\" title=\"main.cpp\">\r\n#include &lt;iostream&gt;\r\n\r\nstruct Base\r\n{\r\n  virtual void method() { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; std::endl; }\r\n};\r\n\r\nstruct Child : public Base\r\n{\r\n  virtual void method() { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; std::endl; }\r\n};\r\n\r\nstruct GrandChild : public Child\r\n{\r\n  virtual void method() { std::cout &lt;&lt; __PRETTY_FUNCTION__ &lt;&lt; std::endl; }\r\n};\r\n\r\nint main()\r\n{\r\n  Base* obj = new GrandChild;\r\n  obj-&gt;method();\r\n  delete obj;\r\n  return 0;\r\n}\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n$ g++ --version\r\ng++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609\r\n$ g++ main.cpp -g\r\n\r\n$ gdb a.out \r\nGNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1\r\n(gdb) b 22\r\nBreakpoint 1 at 0x4009eb: file main.cpp, line 22.\r\n(gdb) r\r\nvirtual void GrandChild::method()\r\n\r\nBreakpoint 1, main () at main.cpp:22\r\n22\t  delete obj;\r\n\r\n(gdb) p &amp;obj\r\n$1 = (Base **) 0x7fffffffdcf8\r\n(gdb) p obj\r\n$2 = (Base *) 0x614c20\r\n(gdb) p *obj\r\n$3 = {_vptr.Base = 0x400bf8 &lt;vtable for GrandChild+16&gt;}\r\n(gdb) p sizeof(obj)\r\n$4 = 8\r\n(gdb) x\/2x obj\r\nx614c20:\t0x00400bf8\t0x00000000\r\n(gdb) info symbol 0x400bf8\r\nvtable for GrandChild + 16 in section .rodata of \/&lt;path_to_binary&gt;\/a.out\r\n(gdb) x\/2x 0x00400bf8\r\n0x400bf8 &lt;_ZTV10GrandChild+16&gt;:\t0x00400aae\t0x00000000\r\n(gdb) info symbol 0x00400aae\r\nGrandChild::method() in section .text of \/&lt;path_to_binary&gt;\/a.out\r\n\r\n(gdb) info vtbl obj\r\nvtable for &#039;Base&#039; @ 0x400bf8 (subobject @ 0x614c20):\r\n[0]: 0x400aae &lt;GrandChild::method()&gt;\r\n<\/pre>\n<p>The object (obj @ 0x614c20) contains a pointer to the vtable (@ 0x400bf8) of its class. The vtable contains the pointers to the actual methods (GrandChild::method() @ 0x400aae).<\/p>\n<pre>\r\nStack\r\n    Addr: 0x7fffffffdcf8 | Val: 0x614c20 (objPtr)\r\n                                    |\r\n               \/--------------------\/\r\nHeap           \u2193\r\n    Addr: 0x614c20       | Val: 0x400bf8 (obj)\r\n                                    |\r\n               \/--------------------\/\r\nData           \u2193\r\n    Addr: 0x400bf8       | Val: 0x400aae (vtable of GrandChild)\r\n                                    |\r\n               \/--------------------\/\r\nText           \u2193\r\n    Addr: 0x400aae     | Val: &lt;asm code&gt; (GrandChild::method())\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Currently most C++ compilers use vtables to realise polymorphism. The object (obj @ 0x614c20) contains a pointer to the vtable<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12144"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12144"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12144\/revisions"}],"predecessor-version":[{"id":16726,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12144\/revisions\/16726"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}