Link to home
Start Free TrialLog in
Avatar of javanic
javanic

asked on

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
Avatar of drichards
drichards

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.
Avatar of javanic

ASKER

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
Avatar of javanic

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial