Hi,
I was given this class member function that overloads the == operator. I don't understand it. Please could you help me understand it?
Here is the class
class Word
{
public:
// constructor
Word(const string& word);
// overloads is-equal-to (or equivalence) operator - this is very useful for testing,
// as we can compare words directly
bool operator==(const Word& rhs) const;
bool isQueryable() const;
private:
string _word;
};
Here is the function:
// overloads the equivalence operator which allows two Words to be compared using ==
bool Word::operator==(const Word& rhs) const
{
if (_word == rhs._word)
return true;
else
return false;
}
So is the function parameter an object (rhs) of class Word. If it is, then what is the if statement comparing and how? If _word belongs to rhs on the RHS, then what object does _word on the LHS belong to? I plan on testing this code with gTest, as in:
TEST(Word, identicalWordsAreEqual) {
Word word1("that");
Word word2("that");
EXPECT_TRUE(word1 == word2);
}