C++: How to make comparison function for char arrays?
Posted
by
Newbie
on Stack Overflow
See other posts from Stack Overflow
or by Newbie
Published on 2011-01-12T15:47:54Z
Indexed on
2011/01/12
15:53 UTC
Read the original article
Hit count: 136
c++
|operator-overloading
Is this possible? i get weird error message when i put char as the type:
inline bool operator==(const char *str1, const char *str2){
// ...
}
Error message: error C2803: 'operator ==' must have at least one formal parameter of class type
... which i dont understand at all.
I was thinking if i could directly compare stuff like:
const char *str1 = "something";
const char *str2 = "something else";
const char str3[] = "lol"; // not sure if this is same as above
and then compare:
if(str1 == str2){
// ...
}
etc.
But i also want it to work with:
char *str = new char[100];
and:
char *str = (char *)malloc(100);
I am assuming every char array i use this way would end in NULL character, so the checking should be possible, but i understand it can be unsafe etc. I just want to know if this is possible to do, and how.
© Stack Overflow or respective owner