you use this if the two classes don't have any form of inherency, you could for instance convert the class car to class money, which would turn the car into cash invoked like: (class money)mercedes . In case of inherency you explicitly cast it to either the base class or any other derived class.
In the time that i've been programming, which is about 20 years now, i didn't see it use much or failed to see the point. Nonetheless i'm interested in where you would like to use such a contruct.
os64dev wrote:In the time that i've been programming, which is about 20 years now, i didn't see it use much or failed to see the point. Nonetheless i'm interested in where you would like to use such a contruct.
My POV is (ok, I can't show up with years of professional work) that conversion operators hide away important details in most cases, it's the same as with overloaded operators when they are abused. IMO, the Java-like style (using methods like "toClassFoo()") is the most readable.
os64dev wrote:
In the time that i've been programming, which is about 20 years now, i didn't see it use much or failed to see the point. Nonetheless i'm interested in where you would like to use such a contruct.
Neo wrote:So is the technical term for this a "conversion operator" or is there some other name for this?
All I know is that Bjarne Stroustrup uses the term in his books. There's also a definition of the term in the glossary section of his website:
conversion operator - operator function specifying a conversion from a user-defined type to either another user-defined type or a built-in type. Note that constructors cannot define conversions to built-in types.
The C++ Programming Language 11.4
The Design and Evolution of C++ 3.6.3
Whether there are any other names for it I frankly don't know..