Comparison Operator Overloading
I have created a class for which I would like to overload the == operator. The name of the class is String. The problem I am running into comes into play when the object is a pointer..
String *a = new String();
if (a == "") return true; //error in VC++
the following operators are defined in the string class..
class String
{
bool operator ==(char *);
friend bool operator==(String &, char *);
}
I tried adding:
friend bool operator==(String *, char *);
but I get a compiler error.. What to do?
Using operator == for string comparison is BAD
It is a BAD idea to use == operator for string comparison. The reason is obvious:
if (mystring == "hello world!") {//...}
Why it is bad? There is ambiguity exactly what do you mean two strings are "equal"? It is unfortunate that alphabets of most languages have lower case and upper case. When comparing two strings, some times case doesn't matter, some times you do care about the case. Using a "==" buries that information and leaves ambiguity there.
It is better to use traditional strcmp, stricmp, strncmp etc.
Re: Using operator == for string comparison is BAD
Quote:
Originally posted by Anthony Mai
It is a BAD idea to use == operator for string comparison. The reason is obvious:
if (mystring == "hello world!") {//...}
Why it is bad? There is ambiguity exactly what do you mean two strings are "equal"?
The ambiguity is when someone is creating home-made classes and there is no written rules as to what is meant by "==".
For std::string, operator == is well defined -- the comparison is done lexicographically according to the current character traits ("The C++ Standard Library", Josuttis, 11.2.7).
Regards,
Paul McKenzie
There is such thing "Well Documented"
Quote:
The behavior of "==" for std::string is for "standard" cases, but those standard cases are well-documented. It is not ambiguos as to what will be returned when you call std::string "==" operator.
Some where on planet earth and on some document, the behavior of "==" for std:string is well documented. Some where on some document some one's home made "==" override is also "well_documented". I am pretty sure that 90% of all source code written in planet earth are all well documented in some document some where.
"Well documented" helps, but it can not become an excuse for using ambiguous code. Document a piece of ambiguous code, unless you are directly commenting next to the source code, does NOT make a piece of code unambiguous.
How many of you memorized the whole of stdC++ document? How many memorize every thing 100% correct? None.
How often do people look into a document to check to see if he/she memoried something correctly? That seldon happen. If you think you know something (while you actually know the wrong answer), you normally don't bother read the document. The result are bugs caused by misunderstanding.
In any case, using CompareNoCase() beats using the ambiguous "==" a million times.
BTW, Paul, I tried to look up where in the document it says operator== for std:string is for lexicographically ordering, within the documentation provided with my compiler. Could not find it. std:string is just a special case of basic_string template where the object is a char.
If an average programmer needs to buy a specific book and wait several days for shipping to check exactly what an operator== is documented as, you might as well try something that makes itself clear by name.
Re: There is such thing "Well Documented"
Quote:
Originally posted by Anthony Mai
Some where on planet earth and on some document, the behavior of "==" for std:string is well documented.
Funny, I found it right here on my desk.
Quote:
How many of you memorized the whole of stdC++ document? How many memorize every thing 100% correct? None.
That's why you have reference material.
Quote:
How often do people look into a document to check to see if he/she memoried something correctly? That seldon happen. If you think you know something (while you actually know the wrong answer), you normally don't bother read the document. The result are bugs caused by misunderstanding.
OK, so what? That doesn't change the fact that "==" for std::string is not ambiguous. All you described is a person who is overconfident of his/her work and thinks they know their material when they really don't know it. Is the documentation of strcpy() ambiguous if someone codes it without knowledge of the terminating NULL byte?
Quote:
In any case, using CompareNoCase() beats using the ambiguous "==" a million times.
Funny, because CompareNoCase() is not the same as what std::string "==" does. So you're right -- it does "beat it" because it does something different than std::string "==". If you were using std::string "==" for case insensitive comparison's then you are not using the right function. CompareNoCase() is one of the cases that need to be implemented explicitly.
Quote:
BTW, Paul, I tried to look up where in the document it says operator== for std:string is for lexicographically ordering, within the documentation provided with my compiler. Could not find it. std:string is just a special case of basic_string template where the object is a char.
Then stop looking at lame compiler documentation and start getting good material to reference from. If you are relying on compiler docs for your reference to the C++ standard library, you'll be disappointed.
Quote:
If an average programmer needs to buy a specific book
If an average programmer has no books on whatever he is programming in, then they are not average, but below average. Any book on the standard library is better than no book, and would explain what std::string is.
Quote:
...and wait several days for shipping to check exactly what an operator== is documented as, you might as well try something that makes itself clear by name.
Is the documentation for std::string so clandestine that I'm the only one that knows it or have it available?
Bottom line -- all of this does not take away from what I'm saying, std::string "==" is not ambiguous. That is all I'm saying. Nothing more, nothing less.
Regards,
Paul McKenzie