Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bool operator in c++

Tags:

c++

Here is a snippet of the code that I found on my beginner file:

struct TriIndex       //triangle index?
{
    int vertex;       //vertex
    int normal;       //normal vecotr
    int tcoord;       //

    bool operator<( const TriIndex& rhs ) const {                                              
        if ( vertex == rhs.vertex ) {
            if ( normal == rhs.normal ) {
                return tcoord < rhs.tcoord;
            } else {
                return normal < rhs.normal;
            }
        } else {
            return vertex < rhs.vertex;
        }
    }
};

I've never seen a bool operator inside a struct before. Can anyone explain this to me?

like image 652
JayC Avatar asked Jun 25 '15 01:06

JayC


2 Answers

TL;DR: The code inside the function is evaluating if *this is < rhs, bool is merely the return type.

The operator is operator < which is the less than operator. The current object is considered the left hand side or lhs, and the object compared against, the right hand of the a < b expression is rhs.

bool  // return type
operator <  // the operator
(const TriIndex& rhs) // the parameter
{
    ...
}

It returns true if the current object is less than (should preceed in containers, etc) the object after the < in an expression like:

if (a < b)

which expands to

if ( a.operator<(b) )

There is a bool operator:

operator bool () const { ... }

which is expected to determine whether the object should evaluate as true:

struct MaybeEven {
    int _i;
    MaybeEven(int i_) : _i(i_) {}
    operator bool () const { return (_i & 1) == 0; }
};

int main() {
    MaybeEven first(3), second(4);
    if (first) // if ( first.operator bool() )
        std::cout << "first is even\n";
    if (second) // if ( second.operator bool() )
        std::cout << "second is even\n";
}
like image 93
kfsone Avatar answered Oct 05 '22 23:10

kfsone


  bool operator<( const TriIndex& rhs )

This line of code is defining a comparison of user-defined datatypes. Here bool is the type of the value this definition will return i.e. true or false.


 const TriIndex& rhs 

This code is telling the compiler to use struct object as a parameter.


if ( vertex == rhs.vertex ) {
        if ( normal == rhs.normal ) {
            return tcoord < rhs.tcoord;
        } else {
            return normal < rhs.normal;
        }
    } else {
        return vertex < rhs.vertex;
    }

The above code is defining criteria of the comparison i.e. how the compiler should compare the two when you say struct a < struct b . This is also called Comparison Operator Overloading.


TL-DR; So after defining the operator when you write a code say:

if (a < b) {
.......
}

where a and b are of type struct fam. Then the compiler will do the if else operations inside the definition and use the return value . If return = true then (a < b) is true otherwise the condition will be false.

like image 41
Aman Avatar answered Oct 06 '22 00:10

Aman