JayC
JayC

Reputation: 169

bool operator in 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?

Upvotes: 5

Views: 20426

Answers (4)

Aman
Aman

Reputation: 73

  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.

Upvotes: 2

kfsone
kfsone

Reputation: 24269

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";
}

Upvotes: 4

missimer
missimer

Reputation: 4079

bool operator<( const TriIndex& rhs ) is the "less than" operating (<), bool is the return type of the "less than" operator.

C++ allows you to override operators such as < for structs and classes. For example:

struct MyStruct a, b;
// assign some values to members of a and b

if(a < b) {
    // Do something
}

What does this code do? Well it depends on how the "less than" operator has been defined for MyStruct. Basically, when you do a < b it will call the "less than" operator for that struct with b being rhs (right hand side) or whatever you call the first parameter, although rhs is the typical convention.

Upvotes: 0

Barett
Barett

Reputation: 5948

That code allows you to compare different TriIndexes with the < operator:

TriIndex alpha;
TriIndex beta;

if (alpha < beta) {
   etc;
}

Upvotes: 0

Related Questions