• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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
0
javanic
Asked:
javanic
  • 2
  • 2
1 Solution
 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now