where getPriority() returns a private data member of type int. But when I do this, I get a compiler error with this:
error C2228: left of '.getPriority' must have class/struct/union
Is this not how I would compare two objects of type Patient? Am I using this incorrectly by assuming it refers to the left hand side item of the comparison operator?
The operator< you're defining has a return type of bool, so it follows that this.getPriority() < rhs.getPriority() evaluates to true or false. Hence the if test is redundant and the function is equivalent to:
bool Patient::operator<(const Patient & rhs) const{
return (this->getPriority() < rhs.getPriority());{ // a "{" too much
}
Not a big one.
@hopesfall What andywestken meant is:
You can also use
1 2 3
bool Patient::operator<(const Patient & rhs) const{
return (getPriority() /* It also means this->getPriority(), just shorter */ < rhs.getPriority());
}
Once you've defined the operator <() and operator ==(), you can implement all of the other relational operators simply from #include <utility> and using the rel_ops namespace.
(Warning: this will also automatically give you the relational operators for all other objects with operator< and operator==, just keep in mind that non-template functions take precedence over template functions.