Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. namespace MyApp
  2. {
  3. namespace Interop
  4. {
  5.  
  6. template < class A, class B>
  7. public ref class MyClass
  8. {
  9. public:
  10. MyClass(){}
  11. ~MyClass(){}
  12.  
  13. };
  14.  
  15. }
  16. }
  17.  
  18. class A;
  19. class B;
  20. using namespace MyApp::Interop;
  21.  
  22. void func()
  23. {
  24. MyClass< A,B >^ h;
  25. }
  26.  
  27. // generics_instance_fields1.cpp
  28. // compile with: /clr
  29. // Instance fields on generic classes
  30. using namespace System;
  31.  
  32. generic <typename ItemType>
  33. ref class MyClass {
  34. // Field of the type ItemType:
  35. public :
  36. ItemType field1;
  37. // Constructor using a parameter of the type ItemType:
  38. MyClass(ItemType p) {
  39. field1 = p;
  40. }
  41. };
  42.  
  43. int main() {
  44. // Instantiate an instance with an integer field:
  45. MyClass<int>^ myObj1 = gcnew MyClass<int>(123);
  46. Console::WriteLine("Integer field = {0}", myObj1->field1);
  47.  
  48. // Instantiate an instance with a double field:
  49. MyClass<double>^ myObj2 = gcnew MyClass<double>(1.23);
  50. Console::WriteLine("Double field = {0}", myObj2->field1);
  51.  
  52. // Instantiate an instance with a String field:
  53. MyClass<String^>^ myObj3 = gcnew MyClass<String^>("ABC");
  54. Console::WriteLine("String field = {0}", myObj3->field1);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement