implicit or default method/property of a class

Hi all,

Is there a way to create an implicit or default method or property of a class in Managed C++?

In other words, how do I declare a ToString method so that the two statements below both call the ToString method?
 
  MessageBox::Show(this->myClass->ToString(), S"explicit call to ToString()");
  MessageBox::Show(this->myClass, S"implicit call to ToString()");

TIA,

javanic
LVL 1
javanicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

drichardsCommented:
I cannot think of a way to do this.  The Show method wants a String* parameter so a conversion needs to be done.  Unfortunately, user-defined operators are not invoked on pointer types in C++.  This means that you'd have explicitly call the conversion operator which is no better than having to explicitly call ToString().

You can read about managed C++ operators at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_20.asp.
0
javanicAuthor Commented:
Hmmm. I was hoping there was something in Managed C++ that allowed something like VB.NET default property. See http://abstractvb.com/code.asp?A=1036. I believe C# also allows that functionality. Consider the Item property--which is the default property/method of a collections class. See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskChoosingDefaultPropertyOrMethodForClass.asp

By the way, I just found the reference to the C# functionality, though I knew it existed. I was hoping it existed in MC++ and that someone would have a reference to it... :)

javanic
0
javanicAuthor Commented:
In case anyone is following this thread, I think I have found what I was looking for. Here is the reference to a default property declaration for Managed C++:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodeldefaultpropertyattributeclasstopic.asp

0
drichardsCommented:
You can try it, but I don't believe a default property will give you the functionality you are looking for.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.