Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MyApp
- {
- namespace Interop
- {
- template < class A, class B>
- public ref class MyClass
- {
- public:
- MyClass(){}
- ~MyClass(){}
- };
- }
- }
- class A;
- class B;
- using namespace MyApp::Interop;
- void func()
- {
- MyClass< A,B >^ h;
- }
- // generics_instance_fields1.cpp
- // compile with: /clr
- // Instance fields on generic classes
- using namespace System;
- generic <typename ItemType>
- ref class MyClass {
- // Field of the type ItemType:
- public :
- ItemType field1;
- // Constructor using a parameter of the type ItemType:
- MyClass(ItemType p) {
- field1 = p;
- }
- };
- int main() {
- // Instantiate an instance with an integer field:
- MyClass<int>^ myObj1 = gcnew MyClass<int>(123);
- Console::WriteLine("Integer field = {0}", myObj1->field1);
- // Instantiate an instance with a double field:
- MyClass<double>^ myObj2 = gcnew MyClass<double>(1.23);
- Console::WriteLine("Double field = {0}", myObj2->field1);
- // Instantiate an instance with a String field:
- MyClass<String^>^ myObj3 = gcnew MyClass<String^>("ABC");
- Console::WriteLine("String field = {0}", myObj3->field1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement