Understanding Operator Overloading

Good day all, i have been battling to understand the overloading of the equality operator, please see code snippet below

I have the following interface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Word
{
public:
	// DO NOT MODIFY THE PUBLIC INTERFACE OF THIS CLASS

	// 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;
	// returns true if queryable, false otherwise
	// (according to the brief, words less than 3 characters are not queryable)
	bool isQueryable() const;

private:
	string _word;
};

And I have the following method to overload ==
1
2
3
4
5
6
bool Word::operator==(const Word& rhs) const {
    if (_word == rhs._word)
        return true;
    else
        return false;
}


What i am battling to understand is the follwoing line in the implementation if (_word == rhs._word)

1) doesnt this bit compare the same thing, since it refers to the private member _word of which has only been defined once in the class and can only carry one value?

2) when rhs is passed in, does it somehow make a new copy of the private member and compare it with the value of _word that was previously assigned

3) My friend told me that _word and rhs._word are different entities and that each one carries distinct or different values, he says the implementation is actually something like if (this _word == rhs._word) where the this keyword refers to the current value of _word and rhs._word is the new value

//I know this may be hard to understand.. i am limited in words as English in not my strong suite..
1) doesnt this bit compare the same thing, since it refers to the private member _word of which has only been defined once in the class and can only carry one value?


Each instance of the class has it's own variables:

1
2
3
// 2 different instances
Word a; // <- 'a' has its own _word member
Word b; // <- 'b' has its own _word member that is different from a's 


So no, it is not comparing the same thing. It is comparing this object's _word... with the _word of whatever object was on the right side of the == operator.

2) when rhs is passed in, does it somehow make a new copy of the private member and compare it with the value of _word that was previously assigned


Each object has its own copy.... so "yes".
Although no new object is being created when rhs is passed in. So "no".

Remember that there is more than one _word. There is one per instance of the class.

3) My friend told me that _word and rhs._word are different entities and that each one carries distinct or different values, he says the implementation is actually something like if (this _word == rhs._word) where the this keyword refers to the current value of _word and rhs._word is the new value


Your friend is sort of correct. _word and rhs._word are different entities... yes.

But there isn't a "new" value.


EDIT:

An example:

1
2
3
4
5
6
7
8
9
Word foo;  // two different Word objects, each with their own _word member
Word bar;

if( foo == bar ) // <- here...  inside your == operator
  // since foo is on the left, foo is 'this'
  // and since bar is on the right... bar is 'rhs'
  //
  // therefore "_word == rhs._word" is really comparing
  // "foo._word == bar._word" 
Last edited on
Ok, i understand the concept a bit now, each instance has its own member!
1
2
3
// 2 different instances
Word a; // <- 'a' has its own _word member
Word b; // <- 'b' has its own _word member that is different from a's  


So for my case
 
_word == rhs._word


I think word& rhs is another instance of the class and has its own member, does this mean that _word that i will be comparing with may have been initialised elsewhere like in the constructor or some other class member function, am i correct in saying this?

-Thanks for the reply!
Reference. Your Word::operator== takes a reference parameter. The 'rhs' is a reference. Reference is not a separate instance. Reference is an alias; another name.

Lets look at the example again:
1
2
3
4
5
6
7
8
9
Word foo; // this calls constructor of Word and thus initializes foo._word
Word bar; // this calls constructor of Word and thus initializes bar._word

if( foo == bar )  // operators are "syntactic sugar"
// you could write the member function call explicitly:
if ( foo.operator==( bar ) )
// during these calls
// this has address of foo; this == &foo
// rhs is a reference to bar; &rhs == &bar 
Topic archived. No new replies allowed.