Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operators in C and C++
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Relational=== All [[Relational operator|relational]] (comparison) operators can be overloaded in C++. Since [[C++20]], the inequality operator is automatically generated if <code>operator==</code> is defined and all four relational operators are automatically generated if <code>operator<=></code> is defined.<ref>{{cite web|url=https://en.cppreference.com/w/cpp/language/operators#Comparison_operators|title=Operator overloading§Comparison operators|website=cppreference.com}}</ref> {| class="wikitable" style="width:100%" ! colspan="2" rowspan="2" | Operation ! rowspan="2" | Syntax ! rowspan="2" | In C ! colspan="2" | C++ prototype |- ! in class K ! outside class |- | {{rh}} colspan="2" | Equal to | align="center" | <code>a '''==''' b</code> | {{yes}} | {{cpp|1=bool K::operator ==(S const& b) const;}} | {{cpp|1=bool operator ==(K const& a, S const& b);}} |- | {{rh}} colspan="2" | Not equal to | style="text-align:center;" | <code>a '''!=''' b</code> || {{yes}} | {{cpp|1=bool K::operator !=(S const& b) const;}} | {{cpp|1=bool operator !=(K const& a, S const& b);}} |- | {{rh}} colspan="2" | Greater than | style="text-align:center;" | <code>a '''>''' b</code> || {{yes}} | {{cpp|1=bool K::operator >(S const& b) const;}} | {{cpp|1=bool operator >(K const& a, S const& b);}} |- | {{rh}} colspan="2" | Less than | style="text-align:center;" | <code>a '''<''' b</code> || {{yes}} | {{cpp|1=bool K::operator <(S const& b) const;}} | {{cpp|1=bool operator <(K const& a, S const& b);}} |- | {{rh}} colspan="2" | Greater than or equal to | style="text-align:center;" | <code>a '''>=''' b</code> || {{yes}} | {{cpp|1=bool K::operator >=(S const& b) const;}} | {{cpp|1=bool operator >=(K const& a, S const& b);}} |- | {{rh}} colspan="2" | Less than or equal to | style="text-align:center;" | <code>a '''<=''' b</code> || {{yes}} | {{cpp|1=bool K::operator <=(S const& b) const;}} | {{cpp|1=bool operator <=(K const& a, S const& b);}} |- | {{rh}} colspan="2" | [[Three-way comparison]]<ref name="threewaycomparison" group="lower-alpha"/>{{efn|Possible return types: <code>std::weak_ordering</code>, <code>std::strong_ordering</code> and <code>std::partial_ordering</code> to which they all are convertible to.}} | style="text-align:center;" | <code>a '''<=>''' b</code> || {{no}} | {{cpp|1=auto K::operator <=>(const S &b);}} | {{cpp|1=auto operator <=>(const K &a, const S &b);}} |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)