Archived

This topic is now archived and is closed to further replies.

Woody FX

operator CString( );

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites