Sign in to follow this  
SymLinked

C++: Operator overloading the == operator.

Recommended Posts

Hi guys, I've been playing around with a FileName class. What it does or why it's there is another topic, but I want to be able to compare two of them. So I got this overload:
bool operator == (const FileName &name);
And that works. But as soon as I introduce another overload:
operator const char *() const;
The == operator doesn't get called anymore. It seems to use the const char one instead and just compare pointers. Is there a way around this, other than to obviously remove the const char operator? Doesn't std::string do this? Thanks in advance!

Share this post


Link to post
Share on other sites
This is why implict cast operations are considered dangerous. I suspect what is happening is you have an implicit constructor taking a const char pointer, and you are doing this:

FileName foo = getSomeFileName();
if(foo == "foo.file")
{
// ...
}

Before, the foo.file was converted to a foo instance and the comparison performed, now the foo is implicitly cast to a char pointer and the pointers compared. That, or your non-member operator==() means that the compiler won't try to convert the left hand into a FileName instance.

Consider why std::string lacks an implicit cast to char pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
This is why implict cast operations are considered dangerous. I suspect what is happening is you have an implicit constructor taking a const char pointer, and you are doing this:

FileName foo = getSomeFileName();
if(foo == "foo.file")
{
// ...
}

Consider why std::string lacks an implicit cast to char pointer.


Still, I was hoping the == operator would be used before the casts operators.. :(

Implicit cast as in coercion? I actually was under the impression std::string had one, but obviously that was wrong. That answers my question, then. Thank you rip-off!

Share this post


Link to post
Share on other sites
That wasn't it. Must be a combination of the implicit cast and some constructor because I can't reproduce it with this class either:


class TestClass
{
public:
bool operator == (const char *bla)
{
return true;
}
operator const char * ()
{
return 0;
}
};

Share this post


Link to post
Share on other sites
It might be calling the constructor implicitly


class FileName{
public:
FileName(const char* name){
cout << "Constructor called";
};
bool operator==(const FileName& rhs){
cout << "Equality operator called";
return true;
}

};

int main(){
FileName f = "foo";
f == "bar";

}


The first line in main() calls the constructor implicitly
The second line tries to compare f to "bar", and it does so by implicitly constructing "bar" into a FileName object and then calling the == operator.

Now have a look at this:

http://stackoverflow.com/questions/1384007/conversion-constructor-vs-conversion-operator-precedence

Does this explain the behavior you're getting?

Edit: I get a compiler error when I add a cast operator to const char*, as the call is ambiguous. Adding the explicit keyword to the constructor solves the problem as then the compiler can only interpret f == "bar" as a char pointer comparison.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this