concepts debugged

I have a question regarding a simple C++ concept.

What is the difference between these two lines of code?

Distance Distance::operator + (Distance 2)

Distance operator + (Distance d1, Distance d2)

I understand how to make each of them work of course, but
they seem to have different purposes.
Anyone know??
cheers!
Distance Distance::operator + (Distance 2) is an out-of-line member definition.

Distance operator + (Distance d1, Distance d2) is a free function.

With the first one, the left hand side cannot be implicitly converted to type Distance. However, this restriction is removed with the second form. The implicit conversion is always permitted for the right hand side.
ah I see... what would be the advantage of implicitly converting to type distance. They seem
to have the same behavior. Is Distance d1 really changing?
Also, when I add the friend function, what advantage is there?

as in...

friend Distance operator + (Distance, Distance)
For your second question:
friend simply makes it so the function can see the class's private member variables and functions. If your class is all public anyway, it makes no difference.

By putting friend, you're saying that it's okay for this particular function to see your private data, but in general it's still hidden/abstracted.
Implicit conversions take place when your class has an implicit constructor that accepts the type used with the operator. Compare these two programs:
http://ideone.com/jv38ko - right hand side allows implicit conversions, left hand side does not
http://ideone.com/tE2QkX - both sides allow implicit conversions
Those two examples explain well. In a way this helps with the order of operations involving mathematical operations. I have been using both ways for a long time. I finally realized that I had no idea what the difference was between them. back to back examples really help.

Cheers!
Topic archived. No new replies allowed.