Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
Quasar

Conversion operator vs operator = vs copy constructor?

Recommended Posts

Suppose my class has all of these:

SomeClass(int i);
SomeClass(const SomeClass &other);
SomeClass &operator = (int i);
operator int();
The compiler would never do something as stupid as converting a SomeClass & to int and using the int constructor instead of the copy constructor... right? ....

Share this post


Link to post

It would never use the int constructor. However, I'm not so sure about the assignment operator. Common sense tells me it makes no sense but I have seen too much nonsensical compiler behavior already.

I'd rather explicitly add the assignment operator just to be safe.

Share this post


Link to post
Graf Zahl said:

It would never use the int constructor. However, I'm not so sure about the assignment operator. Common sense tells me it makes no sense but I have seen too much nonsensical compiler behavior already.

I'd rather explicitly add the assignment operator just to be safe.

Tricky business really. I better think it through and/or come up with a test program before I go any further.

Seems a lot of people are not in favor of using conversion operators. I guess I can see why, because they can go off in unexpected situations.

Share this post


Link to post

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  
×