Computing.Net > Forums > Programming > C++ operator== overload

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ operator== overload

Reply to Message Icon

Name: Guy
Date: November 30, 2002 at 10:25:46 Pacific
OS: Linux + XP
CPU/Ram: Various
Comment:

Hi guys - I see a lot of examples in books, on the net, and in STL headers that overload == like this:

inline bool operator==(AClass lhs, AClass rhs)
{
// Return an appropriate value
}

Some of the examples use pass by reference rather than pass by value.

However, when I try this, I get howls from my compilers indicating that operator== can only take one parameter.

I can get around the problem by either:

a)making the function a friend, and supplying the logic in open/free code.

b)having the function take a single parameter, and supplying the logic inline.

So ... are the examples I am seeing wrong due to current specs? Or am I missing some other piece of knowledge?

Thanks for any advice, Guy



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff J
Date: November 30, 2002 at 14:03:06 Pacific
Reply:

Hello Guy,

I think you might be refering to examples defined outside of any class ("global" and usually 'friend'), as opposed to internal methods within a class. An outside-of-class definition like:

bool operator==(AClass &lhs, AClass &rhs);

implemented as a class method would look like:

bool operator==(ThisClass &rhs);

Since the second function is a class member, there is no longer any need for a left-hand-side parameter, since it is assumed to always be an instance of the class itself (the compiler recognises it would be redundant). Thus, the lhs is already assumed to be 'this', so the compiler baulks if both params are defined.

One minor inconvenience when overloading operators as a member functions, is that the calling object must always be on the lhs. So if for example, class TheClass overloads the == operator, this is legal:

TheClass tcObj(9);
if (tcObj == 9)
//...

but this is not:

if (9 == tcObj)
//...

Likewise with operator+ and others. I've read that is why many STL overloads are done as global friends. It seems most classes that produce objects larger than a few bytes, pass parameters by reference. I forget who on the standards committee, once published a recommendation for writing "friendly classes", which includes passing by ref, always supplying a copy constructor, etc. Whatever, I digress too much.

I would guess the examples you are seeing are just very general, as opposed to specific for a class. Perhaps globals are easier to demonstrate, and it's certainly easier than explaining the assumed 'this' on the lhs.

Cheers


0

Response Number 2
Name: Guy
Date: November 30, 2002 at 15:37:19 Pacific
Reply:

Hi Jeff -

[quote]
Since the second function is a class member, there is no longer any need for a left-hand-side parameter, since it is assumed to always be an instance of the class itself (the compiler recognises it would be redundant). Thus, the lhs is already assumed to be 'this', so the compiler baulks if both params are defined.
[/quote]

Yeah, I (am starting to) understand that.

It does seem to me that all of my references (paperback and net) do not explain this particularly well, and in fact give misleading examples.

I would have figured that Stroustrup at least would have touched on the subject, and I get no joy there.

My understanding comes thru' experimentation with what compiles+runs and what does not, as opposed to a rational explanation in docs somewhere.

If you (or any one else) know of a place where this is documented well, I would appreciate hearing about it (e-mail me).

Regards, Guy


0

Response Number 3
Name: geo
Date: December 1, 2002 at 07:25:12 Pacific
Reply:

When you overload an operator like == there are two rules. The rule you use depends on the right and left hand operands and if they are of the same class or not. For instance:

Dice DiceA();
Dice DiceB();

cout << DiceA;
if(DiceA == DiceB) cout << "They are equal";


in the first cout the classes are different (a member of the ostream class and and a member of the Dice class). In the second cout both operands are of the same Dice class. When both operands are different classes then the operator function accepts 2 parameters (these can be refference or value, except in the case of ostreams which can only be passed as refference). An example based on the above code:

ostream & operator<<(ostream & out, Dice A)
{
out << A.numSides();
}

This is assuming there is a Dice member function numSides() that returns the number of sides as an int. Note: this example would not appear in a class but rather as a function in your projects main file.

When both of the operands are of the same class (as in DiceA == DiceB) then only one parameter is passed to the function and the function has to be a member of the class (in this case Dice). It is assumed that the left hand operand is the object calling the member function. For example:


bool operator==(Dice other)
{
return (other.numSides() == mySides);
}

If this was the class member function and you were to use a statement like (DiceA == DiceB) then DiceB would be the parameter passed. All DiceB's private variables would still be inaccessable. However, since this function is being called within the DiceA object (because it was the left hand operand), DiceA has access to all it's private variables (in the example mySides is a private member variable that is returned when the public member function numSides() is called).

It is not really that hard to grasp this concept and in fact it is not really necessary to use it. You can do everything you normally would do without operator functions by calling class member functions. It is just a convenience for the programmer.

-Geo


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ operator== overload

c++ operator>> overloads www.computing.net/answers/programming/c-operator-overloads/4556.html

Java operator overloading www.computing.net/answers/programming/java-operator-overloading/10191.html

Operator overloaded.... www.computing.net/answers/programming/operator-overloaded/2181.html