{"id":4288,"date":"2014-03-30T09:43:26","date_gmt":"2014-03-30T09:43:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/operator-in-derived-class-never-gets-called-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:43:26","modified_gmt":"2014-03-30T09:43:26","slug":"operator-in-derived-class-never-gets-called-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/operator-in-derived-class-never-gets-called-collection-of-common-programming-errors\/","title":{"rendered":"Operator== in derived class never gets called-Collection of common programming errors"},"content":{"rendered":"<p>Can someone <i>please<\/i> put me out of my misery with this? I&#8217;m trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here&#8217;s my Base and Derived class:<\/p>\n<pre><code>class Base { \/\/ ... snipped\n  bool operator==( const Base&amp; other ) const { return name_ == other.name_; }\n};\n\nclass Derived : public Base { \/\/ ... snipped\n  bool operator==( const Derived&amp; other ) const { \n    return ( static_cast( *this ) ==\n             static_cast( other ) ? age_ == other.age_ :\n                                                 false );\n};\n<\/code><\/pre>\n<p>Now when I instantiate and compare like this &#8230;<\/p>\n<pre><code>Derived p1(\"Sarah\", 42);\nDerived p2(\"Sarah\", 42);\nbool z = ( p1 == p2 );\n<\/code><\/pre>\n<p>&#8230; all is fine. Here the operator== from Derived gets called, but when I loop over a list, comparing items in a list of pointers to Base objects &#8230;<\/p>\n<pre><code>list coll;\n\ncoll.push_back( new Base(\"fred\") );\ncoll.push_back( new Derived(\"sarah\", 42) );\n\/\/ ... snipped\n\n\/\/ Get two items from the list.\nBase&amp; obj1 = **itr;\nBase&amp; obj2 = **itr2;\n\ncout<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Can someone please put me out of my misery with this? I&#8217;m trying to figure out why a derived operator== never gets called in a loop. To simplify the example, here&#8217;s my Base and Derived class: class Base { \/\/ &#8230; snipped bool operator==( const Base&amp; other ) const { return name_ == other.name_; } [&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-4288","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4288","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=4288"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4288\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}