{"id":17229,"date":"2024-03-24T14:16:55","date_gmt":"2024-03-24T14:16:55","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=17229"},"modified":"2025-08-25T14:52:46","modified_gmt":"2025-08-25T14:52:46","slug":"virtual-vs-template","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=17229","title":{"rendered":"Virtual functions vs. Templates (C++)"},"content":{"rendered":"<p><strong>Virtual functions<\/strong><\/p>\n<p>They get resolved at run-time, leading to a shorter compile-time and less binary code.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n\r\nclass Animal\r\n{\r\npublic:\r\n    virtual void makeSound() = 0;\r\n};\r\n\r\nclass Cow : public Animal\r\n{\r\npublic:\r\n    void makeSound() { std::cout &lt;&lt; &quot;Moo&quot; &lt;&lt; std::endl; };\r\n};\r\n\r\nint main()\r\n{\r\n    Cow cow;\r\n    cow.makeSound();\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Templates<\/strong><\/p>\n<p>They get resolved at compile-time, leading to a shorter run-time and more binary code.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n\r\ntemplate &lt;typename T&gt;\r\nclass Animal\r\n{\r\npublic:\r\n    void makeSound() { t.makeSound(); };\r\n\r\nprivate:\r\n    T t;\r\n};\r\n\r\nclass Cow\r\n{\r\npublic:\r\n    void makeSound() { std::cout &lt;&lt; &quot;Moo&quot; &lt;&lt; std::endl; };\r\n};\r\n\r\nint main()\r\n{\r\n    Animal&lt;Cow&gt; cow;\r\n    cow.makeSound();\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Virtual functions They get resolved at run-time, leading to a shorter compile-time and less binary code. Templates They get resolved<\/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":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17229"}],"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=17229"}],"version-history":[{"count":16,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17229\/revisions"}],"predecessor-version":[{"id":19101,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17229\/revisions\/19101"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17229"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}