Strings in Cocoa: Part I
Subject:   operator overloading
Date:   2001-07-03 15:30:42
From:   puppybane
Response to: operator overloading

It depends on the language. In c++, operator overloading is done by declaring a function classname::operator==(const classname * isItEqualToThis)
(I may be wrong about the exact syntax--it's been several years since I used c++)

Operator overloading is a neat feature to have, but it can cause problems. It can make the programmer's life easier (can use ==, etc instead of writing out a function call), but it also encourages bad style. For instance:

MyStringClass *string1 = "blah";
MyStringClass *string2;

string1 = string2;

This code sequence could do one of a number of things, depending on how the implementor wrote the class. It could put the string, "blah" into string2. Or, it could set string2 to point to string1! Or even worse, the implementor could have made "=" the operator for equality, and string1 = string2 could just be a comparison of the two strings!

While a useful ability, operator overloading isn't significantly easier than writing:

[myString isEqualTo:myOtherString]
[myString initWithString:myOtherString]

And this method makes the code easy to understand, and unambiguous.

1 to 1 of 1
  1. operator overloading
    2001-07-03 17:05:01  canyonrat [View]

1 to 1 of 1