Normally you can compare strings..
if (var1 == "a string") ...
But when I declare my own class with the conversion operator, like this :
class my_type {
operator std::string() const { ... };
....
}
Now this :
std::string var1("a string");
my_type x("a string");
if (x == "a string") ....
if (x == var1) ....
does not work.. i.e….
error: no match for ‘operator==’
Of course this works :
if ((std::string) x == var1) ....
But I want it to happen w/o explicitly casting.
why does not c++ convert my_type to string for comparison..
How can I force it to do this w/o implementing the “==” operator itself ?
The same goes for the other comparison operators.
thank you
PS> Btw if I implement operator which convert my_type to numbers(it is OK for my type)…like :
operator double() const { ... };
the comparison with numbers work ok, I don’t need to implement ==,etc…..
As pointed out here, this isn’t about implicit casts, but of how
operator ==behaves on strings.I however suggest you overload
operator ==for your class and not rely on an implicit conversion.