So I'm writing my own custom String class in C++. All has gone relatively well until I needed to overload some comparison operators.
I've attempted to modify the overloaded operator a few different ways but it either ends up telling me it's too ambiguous or just won't accept the parameters. My understanding of operator overloading is that in a statement such as "a < b", it's the right side that's accepted as a parameter.
bool myString::operator<(const myString &right_side) const
{
int val = strcmp(value, right_side.data());
if (val < 0)
{
return true;
}
else
{
return true;
}
}
Here is a working overloaded "<". With this I'm able to compare two myString objects like so:
myString s1 = "abc";
myString s2 = "def";
assert(s1 < s2);
I am also able to pass this assert:
myString s1 = "abc";
assert (s1 < "def");
However, I hit a road block with this other particular assert when I realized it wouldn't use the previously defined operator overloading. Notice the order is flipped from the previous example.
myString s1 = "abc";
assert ("def" < s1);
I realize I need to write a second overloaded "<" to rectify this. Again, my limited understanding of overloading so far is that it's the part to the right of the "<" symbol that needs to be accounted for when writing the function. In that case, I've attempted to use
bool myString::operator<(const myString right_side) const
Since it's only taking in a myString object. I assume this will work. The problem is, it also causes an issue with an ambiguous overload in this statement I previously showed you.
assert(s1 < s2);
I've done some research, specifically on the usage of the "explicit" keyword, but my compiler isn't allowing me to use that because "only declarations of constructors and conversion operators can be 'explicit'" according the compiler. I don't really understand what's happening in this case. Any help would be appreciated.