60 lines
1.5 KiB
Plaintext
60 lines
1.5 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Making a comparison operator ``++virtual++`` implies that you want to compare objects of different types by overriding ``++operator==++``, for instance, in a subclass to compare instances of the base class with instances of the subclass. But polymorphic comparison operators are very difficult to get right, and are actually questionable in concept. After all, can two objects with only a few common members really be equal?
|
|
|
|
|
|
This rule raises issues on ``++virtual++`` comparison operators.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,cpp]
|
|
----
|
|
struct Foo {
|
|
virtual bool operator==(const Foo &other) const; // Noncompliant
|
|
virtual bool operator!=(const Foo &other) const; // Noncompliant
|
|
};
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,cpp]
|
|
----
|
|
struct Foo {
|
|
bool operator==(const Foo &other) const;
|
|
bool operator!=(const Foo &other) const;
|
|
};
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#c87-beware-of\--on-base-classes[C.87: Beware of `==` on base classes]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this "virtual" specifier and refactor the code to not require polymorphism for comparison operators.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
virtual keyword
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 5 Aug 2016, 14:42:13 Ann Campbell wrote:
|
|
See what you think [~alban.auzeill]
|
|
|
|
endif::env-github,rspecator-view[]
|