0
struct point {
    int x, y;
    bool operator<(const point &p) {
    if (x == p.x) return y < p.y;
    else return x < p.x; 
    } 
}; 

What does < between operator and (const point &p) signify?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197

1 Answers1

4

What does < between operator and (const point &p) signify?

It's a part of the member function name. It is a definition of a member function which is an overload of the < operator. It is what makes this work:

point a{};
point b{};

if(a < b) { // point::operator<(const point&) is used here.
    // 
}

Note: The member function should really be const qualified because calling the function does not change *this (the lefthand side of <).

bool operator<(const point &p) const {
//                             ^^^^^

... and it's also preferable if it's not even a member function, but a free function (taking two const point&):

bool operator<(const point& lhs, const point &rhs) {
    return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
} 

This is because if point has an implicit conversion constructor, it will allow an instance of a type that is implicitly convertible to point to appear on both the lefthand and righthand side of <. Example:

struct point {
    point(int X, int Y) :x{X}, y{Y} {}
    point(const std::pair<int, int>& pair) : x{pair.first}, y{pair.second} {}

    int x, y;
};

bool operator<(const point& lhs, const point &rhs) {
    return std::tie(lhs.x, lhs.y) < std::tie(rhs.x, rhs.y);
} 

int main() {
    std::pair<int, int> ip{10,20};
    point pnt{10,19};

    // This works with operator< as a member function or a free function:
    if(pnt < ip) std::cout << "pnt < ip\n";

    // This will not work if `operator<` is a member function:
    if(ip < pnt) std::cout << "ip < pnt\n";
//     ^^
// implicitly converted to `point`
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • @NeilButterworth Sidequestion (if you don't mind): What has happened with your SO account? Where has all your rep gone? It says _"Member since 5 months"_ and the only answer it shows that you've provided was back in 2011. It doesn't add up :-) – Ted Lyngmo Jan 11 '23 at 17:36
  • Possibly the same that happened to my previous two accounts. I changed email (because I changed jobs), and had to start new accounts. I smartened up on my current account, and use my own personal email instead of my work email. (And lost all my previous accrued rep. Boo hoo, alligator tears. Oh well.) – Eljay Jan 11 '23 at 17:38
  • @Eljay Ahhh... So one can't change email address on SO? :-( It's odd that it shows a post from Neil from 2011 and _"member since 5 months"_ though. – Ted Lyngmo Jan 11 '23 at 17:40
  • 1
    I think you can change email on SO, but you either need to plan ahead (... I didn't plan ahead), or you need to get tech support involved to fix the account (... I didn't bother tech support). The "2011" on Neil's account is odd. Does Neil have two accounts, and happened to login to an old mostly unused account? – Eljay Jan 11 '23 at 17:49
  • @ted i decided i didn't want to be active on so and that i didn't like a lot of my posts, so i got jeff to remove my posts, at great effort (things may be easier now). these days i am bed-ridden, and feel the urge to make the odd comment, to keep me amused! some things might not have got cleaned up. – Neil Butterworth Jan 11 '23 at 18:04
  • @NeilButterworth ❤️ – Ted Lyngmo Jan 11 '23 at 18:05
  • @NeilButterworth ❤️ – Eljay Jan 11 '23 at 18:09