Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your derived function doesn't have the same signature as the parent operator, so it <em>hides</em> the parent comparison rather than overriding it. This means that you can't make use of the virtual-ness anyway since the static type of the left hand argument determines the function that's called.</p> <p>That's why the normal method for virtual comparison is a non-member equality operator that dispatches to a virtual comparison function in the base class.</p> <p>Do consider your specific need for virtual comparison though as it may be a design smell that there's an alternate design you could use instead.</p> <p>Also note that member comparison (equality in this case) operators should usually be <code>const</code>.</p> <p>EDIT: It seems that you may only care about comparison based on the static type of the left hand argument, which should be an easier problem. In this case your code handles all cases except where the left hand argument is a type implicitly converts to a <code>Base</code> or <code>Derived</code> by some mechanism other than inheritance (conversion operator or converting constructor). If you don't care about those cases, then the member equality is fine.</p> <p>As one final note, if the comparison could be done entirely through the public interface, we (almost) always prefer non-member, non-friend functions regardless of whether or not it's an operator.</p> <p>EDIT2 (A really quick overview of non-member, non-friend):</p> <p>For example suppose your class has a public <code>key</code> method, and if the keys of two instances are equal you want to call the objects equal. Then, without using friends or a member equality operator, you can write your equality standalone:</p> <pre><code>bool operator==(const MyType&amp; left, const MyType&amp; right) { return left.key() == right.key(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload