{"id":4357,"date":"2014-03-30T10:01:59","date_gmt":"2014-03-30T10:01:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/polymorphism-or-inheritance-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:01:59","modified_gmt":"2014-03-30T10:01:59","slug":"polymorphism-or-inheritance-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/polymorphism-or-inheritance-collection-of-common-programming-errors\/","title":{"rendered":"Polymorphism or inheritance?-Collection of common programming errors"},"content":{"rendered":"<p>The function <code>int f(int)<\/code> in class <code>B<\/code> &#8220;hides&#8221; the function with the same name and signature in its base class.<\/p>\n<p>So, when you call <code>b.f(3);<\/code>, and the variable <code>b<\/code> has type <code>B<\/code>, you are calling <code>B::f<\/code>.<\/p>\n<p>Virtual functions are only needed if you want <code>b.f(3)<\/code> to call <code>B::f<\/code> in cases where the type of the <em>variable<\/em> <code>b<\/code> is <code>A&amp;<\/code>, but the <em>object<\/em> it refers to has runtime type <code>B<\/code>. In that situation, the function called would be <code>B::f<\/code> if <code>A::f<\/code> is virtual, but <code>A::f<\/code> is called if non-virtual.<\/p>\n<p>Virtual function calls take the runtime type of objects into account even if they&#8217;re used via a pointer or reference to a base class. But <code>B b; b.f(3)<\/code> is a call to <code>B::f<\/code> regardless of whether <code>A::f<\/code> even exists, never mind whether it&#8217;s virtual or non-virtual.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The function int f(int) in class B &#8220;hides&#8221; the function with the same name and signature in its base class. So, when you call b.f(3);, and the variable b has type B, you are calling B::f. Virtual functions are only needed if you want b.f(3) to call B::f in cases where the type of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4357","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4357"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4357\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}