{"id":7199,"date":"2022-09-07T11:27:41","date_gmt":"2022-09-07T05:57:41","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/"},"modified":"2022-09-07T11:27:41","modified_gmt":"2022-09-07T05:57:41","slug":"solved-do-i-need-a-virtual-function","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/","title":{"rendered":"[Solved] do I need a virtual function"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-39300580\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"39300580\" data-parentid=\"39300240\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>It appears that what you want is for your <code>Derived<\/code> class to support the following interface:<\/p>\n<pre><code>someInfo si;\nsomeMoreInfo smi;\nBase* pB = new Derived;\n\n\/\/ Setting info\npB-&gt;setInfo(si);       \/\/ Set the `Base::c` member of `*pB`\npB-&gt;setInfo(smi);      \/\/ Set the `Derived::g` member of `*pB`\n\n\/\/ Getting info\nsomeInfo pB_si = pB-&gt;getInfo();   \/\/ Get  `Base::c` from `*pB`\nsomeMoreInfo pB_smi = pB-&gt;getInfo();   \/\/ Get  `Derived::g` from `*pB`\n<\/code><\/pre>\n<h3>Setting info: overloading<\/h3>\n<p>You&#8217;re actually <strong>correct<\/strong> that you <strong>don&#8217;t<\/strong> need <code>setInfo<\/code> to be <code>virtual<\/code>, because <code>Derived::setInfo<\/code> doesn&#8217;t <strong>replace<\/strong> the <code>Base<\/code> method; <strong>both<\/strong> are supposed to be accessible.<\/p>\n<p>Unfortunately, as I&#8217;ve noted in a comment, by default <code>Base::setInfo<\/code> gets <strong>hidden<\/strong> by <code>Derived::setInfo<\/code>, <em>regardless<\/em> of the signature (!). Yes, this is surprising behavior; Scott Meyers notes in <em>Effective C++<\/em> that this &#8220;surprises every C++ programmer the first time they encounter it&#8221; (Item 33 of the third edition).<\/p>\n<p>Fortunately, the solution is simple. You need the <code>using<\/code> keyword:<\/p>\n<pre><code>Class child: public Base{\n\n  \/\/ ... private members...\n\n  public:\n    using Base::setInfo;\n    void setInfo(someMoreInfo);\n\n  \/\/ .. rest of class...\n};\n<\/code><\/pre>\n<p>This <em>exposes<\/em> the otherwise hidden name <code>Base::setInfo<\/code>, permitting overload resolution to happen as if <em>both<\/em> versions of <code>setInfo<\/code> were defined inside <code>Derived<\/code>.<\/p>\n<h3>Getting info: return-dependent overload resolution<\/h3>\n<p>C++ <strong>does not support overloading on return types<\/strong>. You can easily check this:<\/p>\n<pre><code>class Foo\n{\n  public:\n    int foo() { return 3; }\n    double foo() { return 83.4; }\n};\n<\/code><\/pre>\n<p>Clang gives the following error (which is much more helpful than the GCC error):<\/p>\n<pre><code>error: functions that differ only in their return type cannot be overloaded\n<\/code><\/pre>\n<p>Now, you might ask, &#8220;why not?&#8221; Indeed, I know of at least one language, Perl, that supports different behavior based on the &#8220;context&#8221; in which an expression is evaluated.<\/p>\n<p>This is actually pretty confusing (at least for those who aren&#8217;t fans of Perl, I guess). In general, programmers <em>don&#8217;t<\/em> expect different functions to be called depending on the <em>context<\/em> in which the function is called. And there are many contexts that don&#8217;t clearly indicate the expected return type:<\/p>\n<pre><code>std::cout &lt;&lt; Foo().foo() &lt;&lt; std::endl;  \/\/ AMBIGUOUS!\n<\/code><\/pre>\n<p>&#8230; so there is simply no solution: you <em>cannot<\/em> differentiate two functions merely in their return type. You <em>must<\/em> provide different names for the two functions.<\/p>\n<p><strong>EDIT:<\/strong> If you really want, you <em>could<\/em> return-by-reference-argument:<\/p>\n<pre><code>void Base::getInfo(someInfo&amp; si)\n{\n    si = c;\n}\nvoid Derived::getInfo(someMoreInfo&amp; smi)\n{\n    smi = g;\n}\n<\/code><\/pre>\n<p>Here, overload-resolution will be applied by checking the type of the argument passed to <code>getInfo<\/code>, and no ambiguity is possible. This is one of the benefits of returning by reference-argument, though it&#8217;s debatable whether this is a superior to simply naming these more explicitly (I think explicit names would be preferable, and I think most programmers would agree).<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved do I need a virtual function <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] It appears that what you want is for your Derived class to support the following interface: someInfo si; someMoreInfo smi; Base* pB = new Derived; \/\/ Setting info pB-&gt;setInfo(si); \/\/ Set the `Base::c` member of `*pB` pB-&gt;setInfo(smi); \/\/ Set the `Derived::g` member of `*pB` \/\/ Getting info someInfo pB_si = pB-&gt;getInfo(); \/\/ Get `Base::c` &#8230; <a title=\"[Solved] do I need a virtual function\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/\" aria-label=\"More on [Solved] do I need a virtual function\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,513,519,527,547],"class_list":["post-7199","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-c11","tag-class","tag-inheritance","tag-oop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] do I need a virtual function - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] do I need a virtual function - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] It appears that what you want is for your Derived class to support the following interface: someInfo si; someMoreInfo smi; Base* pB = new Derived; \/\/ Setting info pB-&gt;setInfo(si); \/\/ Set the `Base::c` member of `*pB` pB-&gt;setInfo(smi); \/\/ Set the `Derived::g` member of `*pB` \/\/ Getting info someInfo pB_si = pB-&gt;getInfo(); \/\/ Get `Base::c` ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-07T05:57:41+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] do I need a virtual function\",\"datePublished\":\"2022-09-07T05:57:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/\"},\"wordCount\":334,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"c++11\",\"class\",\"inheritance\",\"oop\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/\",\"name\":\"[Solved] do I need a virtual function - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-09-07T05:57:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-do-i-need-a-virtual-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] do I need a virtual function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] do I need a virtual function - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] do I need a virtual function - JassWeb","og_description":"[ad_1] It appears that what you want is for your Derived class to support the following interface: someInfo si; someMoreInfo smi; Base* pB = new Derived; \/\/ Setting info pB-&gt;setInfo(si); \/\/ Set the `Base::c` member of `*pB` pB-&gt;setInfo(smi); \/\/ Set the `Derived::g` member of `*pB` \/\/ Getting info someInfo pB_si = pB-&gt;getInfo(); \/\/ Get `Base::c` ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/","og_site_name":"JassWeb","article_published_time":"2022-09-07T05:57:41+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] do I need a virtual function","datePublished":"2022-09-07T05:57:41+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/"},"wordCount":334,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","c++11","class","inheritance","oop"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/","url":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/","name":"[Solved] do I need a virtual function - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-07T05:57:41+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-do-i-need-a-virtual-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] do I need a virtual function"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=7199"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7199\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}