c++ operator casting

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

c++ operator casting

Post 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?
Only Human
User avatar
JoeKayzA
Member
Member
Posts: 79
Joined: Wed Aug 24, 2005 11:00 pm
Location: Graz/Austria

Post 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

Code: Select all

void foo(MyClass1 &obj);
you could do

Code: Select all

MyClass2 obj2;
foo(obj2);
And the compiler should invoke your operator MyClass1() automatically. Else, set an explicit cast.

cheers
Joe
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Post 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
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post 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
Author of COBOS
User avatar
JoeKayzA
Member
Member
Posts: 79
Joined: Wed Aug 24, 2005 11:00 pm
Location: Graz/Austria

Post 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....
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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. ;)
Every good solution is obvious once you've found it.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post 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.
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Post by Neo »

So is the technical term for this a "conversion operator" or is there some other name for this?
Only Human
User avatar
gaf
Member
Member
Posts: 349
Joined: Thu Oct 21, 2004 11:00 pm
Location: Munich, Germany

Post 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
Post Reply