operator CString( );

Started by
3 comments, last by Woody FX 20 years, 9 months ago
I have come across this in a MFC based prog that i''m working on and i cannot find what it means in any of my books. It looks like the CString class is being overloaded? Can somebody please explain what is happening.. it appears in a class definition as public. Thanks for the help!
Advertisement
An operator type(), where ''type'' represents any type, is a casting operator. For instance, if class Banana defines operator Fish(), you will be able to cast a Banana to a Fish.
To clarify what Miserable said, this is an implicit typecast operator. It means that the compiler is free to use it to cast to the target type whenever it makes sense, not just when you explicitly invoke the cast.

Single-argument constructors act as explicit typecast operators.

How appropriate. You fight like a cow.
Wooooww thats making me dizzy!

So is anything being overloaded... i''m not following?

operator CString(); is a an implict typecast operator???


Under what part of C++ should i be lloking for an explantion...

Is is operator overloading and typecasts?

Thanks

quote:Original post by Sneftel
Single-argument constructors act as explicit typecast operators.

I thought it was only if they were marked explicit .

Example for Woody:
void func(CString s);int main(){ MyClass c; // defines operator CString func(c);   // method operator CString called for implicit conversion }

Implicit conversion is generally pretty evil most of the time because you don't see it happening (hence "implicit"). Thats why you don't see it in many standard library classes.

[edited by - antareus on July 2, 2003 6:18:47 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement