Page 1 of 1
c++ operator casting
Posted: Thu Feb 01, 2007 8:51 am
by Neo
I recently came across the following use of the operator overloading
Code: Select all
class MyClass2{
.......
operator MyClass1() const;
.......
};
I think this allows you to cast an object of MyClass2 to MyClass1. Is this right? how do I invoke this operator?
Posted: Thu Feb 01, 2007 9:10 am
by JoeKayzA
AFAIK, you just write some code that requires a cast, and the compiler will at first look for custom cast-operators in the object's class.
so, assuming you have a function/method
you could do
And the compiler should invoke your operator MyClass1() automatically. Else, set an explicit cast.
cheers
Joe
Posted: Thu Feb 01, 2007 10:03 am
by gaf
How do I invoke this operator?
You might try searching for "conversion operator" to get some more information about the topic
cheers,
gaf
Posted: Thu Feb 01, 2007 3:33 pm
by os64dev
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.
regards
Posted: Fri Feb 02, 2007 4:46 am
by JoeKayzA
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.
Just my 2 eurocents however....
Posted: Fri Feb 02, 2007 6:05 am
by Solar
The old issue about whether the fact that something can be abused makes it a bad thing.
ASM, C and C++ on the one side had one answer to that question, Java and C# had the other. Both are good answers in certain conditions.
So much for the objective side. Subjectively, Java enumerations (hasNext() / getNextItem() or what it was again) gives me the creeps.
Posted: Wed Feb 07, 2007 7:27 am
by Neo
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.
regards
I came across it in some of my clients code.
Posted: Wed Feb 07, 2007 7:31 am
by Neo
So is the technical term for this a "conversion operator" or is there some other name for this?
Posted: Thu Feb 08, 2007 7:49 am
by gaf
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..
regards,
gaf