{"id":4480,"date":"2014-03-30T11:25:47","date_gmt":"2014-03-30T11:25:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-operator-overloading-polymorphism-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:25:47","modified_gmt":"2014-03-30T11:25:47","slug":"c-operator-overloading-polymorphism-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/c-operator-overloading-polymorphism-collection-of-common-programming-errors\/","title":{"rendered":"c++ operator overloading &amp; polymorphism-Collection of common programming errors"},"content":{"rendered":"<p>First, polymorphism works with both references and pointers. And operator overloading works with references. So there&#8217;s no problem at that level.<\/p>\n<p>There is a potential problem with binary operators. Direct language support for polymorphism on an operator only works on the left hand operand. Where as for something like binary <code>+<\/code>, one logically would expect double dispatch. While this can be implemented, it is somewhat more complex, especially if the hierarchies are open.<\/p>\n<p>For operators like binary <code>+<\/code>, which normally return a new object, there is also the question of the return type. Typically, this <em>can&#8217;t<\/em> be a reference, since there is no object with an appropriate type to refer to. Patterns like the letter-envelop idiom have been developed to deal with this, but they aren&#8217;t necessarily simple, and they often have very significant run-time overhead. <em>Or<\/em> the overloaded operator returns a special type, which only saves its arguments, and knows how to calculate the value with the correct type when requested.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First, polymorphism works with both references and pointers. And operator overloading works with references. So there&#8217;s no problem at that level. There is a potential problem with binary operators. Direct language support for polymorphism on an operator only works on the left hand operand. Where as for something like binary +, one logically would expect [&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-4480","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4480","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=4480"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4480\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}