-1

I saw the following code:

struct Point
{
    int x,y;
    bool operator <(const Point &p) const {
        return x<p.x||(x==p.x&&y<p.y);
    }
};

I don't understand what this thing is:

 bool operator <(const Point &p) const {
      return x<p.x||(x==p.x&&y<p.y);
 } 

What is it?

James Custer
  • 857
  • 6
  • 12
Raj Singhania
  • 333
  • 1
  • 3
  • 5

4 Answers4

7

Look, it's very simple.

You've defined a struct representing a point. Assuming you have two such structs, the code in question defines a comparison operator for them, so that you can write something like

if (pt1 < pt2) { ...

How can the compiler treat the < sign? In C++ it's doing the following: the expression pt1 < pt2 gets interpreted as pt1.operator < (pt2). That is, the method with special name operator < of object pt1 is called, with argument equal to pt2.

Let's see what it is doing. const Point &p in its argument denotes the other operand being compared. const says that we are not going to modify the operand (this allows the compiler to produce more effective code), and & means that we are not copying the struct passed, but working with actual Point instance (this is faster again, as no copying is done for the comparison).

Now, the expression x<p.x checks whether the x value at the left-hand operand (it's the this object, that is, the object in context of which the method is running)* against the x of the right-hand operand. Next, the code checks the case when the left-hand x is equal to the right-hand one, so the y's are compared.


*For language purists: the wording is not precise, but for the beginner-oriented explanation it should be ok.

Vlad
  • 35,022
  • 6
  • 77
  • 199
2

It's a definition for an operator, in this particular case operator <. It's what gets called when someone does:

Point a, b;
if( a < b )
{ 
    //...
}
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

It's a member function that overloads the < operator

Point a,b;
a < b; // the member function is used here.
bames53
  • 86,085
  • 15
  • 179
  • 244
  • Are you sure it overloads? That would imply a default less than operator is implicitly provided by the compiler otherwise ... – AJG85 May 30 '12 at 19:37
  • 2
    @AJG85: No, it implies that you are creating another function with the name `<`, which is differentiated by its parameters, and that's exactly what is happening. – Benjamin Lindley May 30 '12 at 19:39
  • It's just a nit-picky thing. I guess you can still call it operator overloading even if it's not a function overload and that operator would be undefined otherwise. – AJG85 May 30 '12 at 19:52
  • @AJG85: But, it is a function overload. operator< is defined, many times over, for built in types as well as user defined types. Perhaps you are confusing overriding with overloading. Overloading is re-using the same function name multiple times for different functions by giving them different parameters. Overriding is replacing the behavior of a default provided function with a more specialized one, and that seems to be what you are describing. – Benjamin Lindley May 30 '12 at 19:58
  • I was under the impression overloading is defining a function with the same name and different parameters *in the same scope* where the right method to be called is determined at compile time. Whereas overriding is redefining a usually virtual method with the same signature from a base class where the correct version to be called is determined at runtime. Right? – AJG85 May 30 '12 at 20:05
  • @AJG85: *"I was under the impression overloading is ..."* -- Exactly correct, and exactly what is happening here. – Benjamin Lindley May 30 '12 at 20:13
  • @Benjamin: IANALL, but for _method_ overloading, which the answer is referring to, the method with the same name but different signature has to be available for the current class, not for some other classes. (_Operator_ overload is however something conceptually different.) – Vlad May 31 '12 at 09:22
1

In C++ structs are very similar to classes. What you see there is an operator definition as instance method. It compares the "this" Point to the (unmodifiable) Point referenced as parameter "p" and is not allowed to modify the "this" Point due to the const after the parameter list.

Wormbo
  • 4,978
  • 2
  • 21
  • 41