< C++ .NET Arrays & Collections 3 | Main | C++ .NET Arrays & Collections 5 >


 

 

Arrays and Collections 4

 

 

What we have in this page?

 

  1. Arrays and Reference Types

 

Arrays and Reference Types

 

Because reference types are always accessed using references, creating and initializing arrays of reference types is slightly different from creating arrays of value types. The following exercise shows how to create and use an array of reference types. In this example, you’ll use System::String as the reference type, but you can easily substitute a reference type of your own.

 

1.        Open Visual C++/Studio, and create a new CLR Console Application project named RefArray.

 

Creating a new C++ .NET CLR Console Application project named RefArray

 

2.        Open the RefArray.cpp source file and try the following code example.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

#define ARRAY_SIZE 3

 

ref class MyClass

{

   public:

      int m_i;

      // Constructor

      MyClass()

      {

         Console::WriteLine("In MyClass...");

      }

};

 

// Returns a managed array of a reference type.

array<MyClass^>^ Test0()

{

   int i;

   Console::WriteLine("In Test0...");

   array< MyClass^ >^ local = gcnew array< MyClass^ >(ARRAY_SIZE);

 

   for (i = 0; i < ARRAY_SIZE; i++)

   {

     local[i] = gcnew MyClass;

     local[i]->m_i = i + 10;

   }

   return local;

}

 

int main(array<System::String ^> ^args)

{

  int i;

  // Declares an array of user-defined reference types

  // and initializes with a function.

  Console::WriteLine("Array of user-defined reference types and initializes with a function");

  array< MyClass^ >^ MyClass0;

  MyClass0 = Test0();

 

  for (i = 0; i < ARRAY_SIZE; i++)

    Console::WriteLine("MyClass0[{0}] = {1}", i, MyClass0[i]->m_i);

  Console::WriteLine();

  // Declares and initializes an array of user-defined

  // reference types.

  Console::WriteLine("Array of user-defined reference types");

  array< MyClass^ >^ MyClass1 = gcnew array< MyClass^ >(ARRAY_SIZE);

  for (i = 0; i < ARRAY_SIZE; i++)

  {

     MyClass1[i] = gcnew MyClass;

     MyClass1[i] -> m_i = i + 20;

  }

  for (i = 0; i < ARRAY_SIZE; i++)

     Console::WriteLine("MyClass1[{0}] = {1}", i, MyClass1[i] -> m_i);

  Console::WriteLine();

  return 0;

}

3.        Compile and run the code. The output is shown below.

 

Reference type array console program output sample

 

The following sample shows how to create single-dimension arrays of reference, value, and native pointer types. It also shows how to return a single-dimension array from a function and how to pass a single-dimension array as an argument to a function. The next sample shows how to perform aggregate initialization. You can use the previous project or create a new one to try these examples.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

#define ARRAY_SIZE 2

 

value struct MyStruct

{

    int m_i;

};

 

ref class MyClass

{

    public:

      int m_i;

};

 

struct MyNativeClass

{

   int m_i;

};

 

// Returns a managed array of a reference type.

array<MyClass^>^ Test0()

{

      int i;

      array< MyClass^ >^ local = gcnew array< MyClass^ >(ARRAY_SIZE);

 

      for (i = 0; i < ARRAY_SIZE; i++)

      {

         local[i] = gcnew MyClass;

         local[i] -> m_i = i;

   }

   return local;

}

 

// Returns a managed array of Int32.

array<Int32>^ Test1()

{

      int i;

      array< Int32 >^ local = gcnew array< Int32 >(ARRAY_SIZE);

 

      for (i = 0 ; i < ARRAY_SIZE ; i++)

       local[i] = i + 10;

      return local;

}

 

// Modifies an array.

void Test2(array< MyNativeClass * >^ local)

{

   for (int i = 0; i < ARRAY_SIZE; i++)

     local[i]->m_i = local[i]->m_i + 2;

}

 

int main(array<System::String ^> ^args)

{

   int i;

   // Declares an array of user-defined reference types

   // and initializes with a function.

   Console::WriteLine("array of user-defined reference types and initializes with a function");

   array< MyClass^ >^ MyClass0;

   MyClass0 = Test0();

 

   for (i = 0; i < ARRAY_SIZE; i++)

     Console::WriteLine("MyClass0[{0}] = {1}", i, MyClass0[i] -> m_i);

   Console::WriteLine();

   // Declares an array of value types and initializes with a function.

   Console::WriteLine("array of value types and initializes with a function");

   array< Int32 >^ IntArray;

   IntArray = Test1();

   for (i = 0; i < ARRAY_SIZE; i++)

     Console::WriteLine("IntArray[{0}] = {1}", i, IntArray[i]);

   Console::WriteLine();

   // Declares and initializes an array of user-defined

   // reference types.

   Console::WriteLine("array of user-defined reference types");

   array< MyClass^ >^ MyClass1 = gcnew array< MyClass^ >(ARRAY_SIZE);

   for (i = 0; i < ARRAY_SIZE; i++)

   {

      MyClass1[i] = gcnew MyClass;

      MyClass1[i] -> m_i = i + 20;

   }

   for (i = 0; i < ARRAY_SIZE; i++)

     Console::WriteLine("MyClass1[{0}] = {1}", i, MyClass1[i] -> m_i);

   Console::WriteLine();

   // Declares and initializes an array of pointers to a native type.

   Console::WriteLine("array of pointers to a native type");

   array< MyNativeClass * >^ MyClass2 = gcnew array<MyNativeClass * >(ARRAY_SIZE);

   for (i = 0; i < ARRAY_SIZE; i++)

   {

      MyClass2[i] = new MyNativeClass();

      MyClass2[i] -> m_i = i + 30;

   }

   for (i = 0; i < ARRAY_SIZE; i++)

     Console::WriteLine("MyClass2[{0}] = {1}", i, MyClass2[i]->m_i);

   Console::WriteLine();

   Test2(MyClass2);

   for (i = 0; i < ARRAY_SIZE; i++)

     Console::WriteLine("MyClass2[{0}] = {1}", i, MyClass2[i]->m_i);

   Console::WriteLine();

   delete[] MyClass2[0];

   delete[] MyClass2[1];

   // Declares and initializes an array of user-defined value types.

   Console::WriteLine("array of user-defined value types");

   array< MyStruct >^ MyStruct1 = gcnew array< MyStruct >(ARRAY_SIZE);

   for (i = 0; i < ARRAY_SIZE; i++)

   {

     MyStruct1[i] = MyStruct();

     MyStruct1[i].m_i = i + 40;

   }

   for (i = 0 ; i < ARRAY_SIZE ; i++)

     Console::WriteLine("MyStruct1[{0}] = {1}", i, MyStruct1[i].m_i);

   return 0;

}

 

Output:

 

 

 

 

 

 

 

----------------------------------------------------

Program output sample that shows how to create single-dimension arrays of reference, value, and native pointer types

 

The following code sample shows how to perform aggregate initialization on single dimension managed arrays.

// RefArray.cpp : main project file.

// compile with: /clr

#include "stdafx.h"

 

using namespace System;

 

ref class G

{

   public:

     G(int i) {}

};

 

value class V

{

   public:

     V(int i) {}

};

 

class N

{

   public:

     N(int i) {}

};

 

int main(array<System::String ^> ^args)

{

      // Aggregate initialize a single-dimension managed array.

      array<String^>^ gc1 = gcnew array<String^>{"one", "two", "three"};

      array<String^>^ gc2 = {"one", "two", "three"};

      array<G^>^ gc3 = gcnew array<G^>{gcnew G(0), gcnew G(1), gcnew G(2)};

      array<G^>^ gc4 = {gcnew G(0), gcnew G(1), gcnew G(2)};

      array<Int32>^ value1 = gcnew array<Int32>{0, 1, 2};

      array<Int32>^ value2 = {0, 1, 2};

      array<V>^ value3 = gcnew array<V>{V(0), V(1), V(2)};

      array<V>^ value4 = {V(0), V(1), V(2)};

      array<N*>^ native1 = gcnew array<N*>{new N(0), new N(1), new N(2)};

      array<N*>^ native2 = {new N(0), new N(1), new N(2)};

      return 0;

}

// No output

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8

 


 

< C++ .NET Arrays & Collections 3 | Main | C++ .NET Arrays & Collections 5 >