{"id":3069,"date":"2021-10-30T21:44:16","date_gmt":"2021-10-31T05:44:16","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=3069"},"modified":"2021-10-30T23:23:22","modified_gmt":"2021-10-31T07:23:22","slug":"rationale-behind-c-commandments-4-method-signature-system-polymorphism","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/rationale-behind-c-commandments-4-method-signature-system-polymorphism\/","title":{"rendered":"Rationale Behind C++ Commandments (4) &#8211; Method Signature System"},"content":{"rendered":"\n<p>Function <em>signature <\/em>system, which allows users to use the same function name in different functions as long as they differ in the combination of <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>input<\/strong> arguments <strong>types<\/strong><\/li><li><code data-enlighter-language=\"cpp\" class=\"EnlighterJSRAW\">const<\/code> modifiers counts as a different input argument type<\/li><li>object <code>const<\/code>-ness (whether it&#8217;s <code>const<\/code>-method or not) &#8211; this only make sense with classes<\/li><\/ul>\n\n\n\n<p>and C++ will figure out what to call by matching the call with the available combinations (signatures). <\/p>\n\n\n\n<p>C does not allow the same function name to be used in different places, so under the hood, it&#8217;s done through name mangling (generating a unique &#8216;under-the-hood&#8217; function name based on the signature). This mechanism has a lot of implications that a professional programmer should observe:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>since C does not mangle its names in the object code, they&#8217;ll need to be wrapped around with <code>extern \u201cC\u201d<\/code> block in a C++ program so C++ won&#8217;t pervert (mangle) their function names with input arguments.<\/li><li>[24] parameter defaulting might be ambiguous with another function that does not have the said parameter (the compiler will cry about it)<\/li><li>[26] access controls\/levels must play no part in resolving signatures because access level must not change the meaning of a program!<\/li><\/ul>\n\n\n\n<p>C++ resolve function overloading using signatures within its local namespace. Function overloading works for both<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>free functions (free functions are at the root namespace), as well as <\/li><li>classes (the name of the class itself is the namespace)<\/li><\/ul>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_3069\" class=\"pvc_stats all  \" data-element-id=\"3069\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Function signature system, which allows users to use the same function name in different functions as long as they differ in the combination of input arguments types const modifiers counts as a different input argument type object const-ness (whether it&#8217;s &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2021\/10\/30\/rationale-behind-c-commandments-4-method-signature-system-polymorphism\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_3069\" class=\"pvc_stats all  \" data-element-id=\"3069\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[25,64],"tags":[],"class_list":["post-3069","post","type-post","status-publish","format-standard","hentry","category-cpp","category-rbcc-series"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=3069"}],"version-history":[{"count":2,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3069\/revisions"}],"predecessor-version":[{"id":3082,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/3069\/revisions\/3082"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=3069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=3069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=3069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}