Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's a short snippet from a search program. I had one doubt about this.
What is x.the_key is being used for? Why can't x and y be checked with x==y here?
Why do we have to the local copy of a key's value?

class Key()
{
public: Key(int x=0);
int the_key()const;
}
bool operator == (const Key &x, const Key &y) // == overloading
{
return x.the_key()==y.the_key(); // x.the_key?
}
Posted
Updated 2-Jan-11 7:33am
v6

1 solution

Comparing two objects via == is something different from comparing the content of these objects. Since you have shown us only the contract for class Key and not its implementation, I can only guess that the function int the_key() returns the integer value that is stored inside class Key.
Overloading the == operator is done here so you can compare the content of two instances of type Key as you would usually compare the two references to the respective instances.

Caveat!: Changing the semantic of the == operator is considered bad design! Please don't do that unless you have a very good reason.

At an superficial glance one would think that two references are being compared, but by overloading the == operator it is now a comparison of their content.
There must be a very, very good reason when this is done and it should be documented really well.

Best Regards,

Manfred
 
Share this answer
 
v3
Comments
optimus_prime1 31-Dec-10 5:26am    
I am confused what's x.the_key(); doing?
int the_key()const; is only defined in the class .h file and there's no more mention of it in the example from a book I am trying the program. It only mentions the_key is being used to inspect a copy of key's value. A little bit more explanation would make this more clear to me.
Thanks!
CPallini 31-Dec-10 5:33am    
Without implementation of the 'theKey' method the program can't compile and I don't know if it deserves further investigation.
Manfred Rudolf Bihy 31-Dec-10 5:45am    
@CPallini: He said it's from a book so I take it that part may have been left as an exercise. We have to give this guy a break at least he is actually reading a book! :)
Manfred Rudolf Bihy 31-Dec-10 5:50am    
@Optimus: x and y are the two operands of type Key for the == operator, so if one were to write the expression x == y what happens behing the scenes is that the function the_key() is called on both operands and the two values (here integers) are compared.
Since this was probably an example on operator overloading they left out the implementation details as they are easy to figure out.
Type Key needs a private field of type integer which is initialized in the constructor call. The function the_key() will return the value of said private instance variable.

Clearer now?
optimus_prime1 1-Jan-11 10:36am    
Hi Manfred,
Thanks for being so cooperative and I really appreciate this. If you'll still allow me to ask, why can't we compare return (x==y) just like this. Why do we even need to use int the_key()const; ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900