C++/CLI supports not only stack-based objects but also heap-based objects, but if you want to interoperate with other CLI languages such as C #, J #, and Visual Basic, it's important to know that these languages support only heap based objects When you are in a heap-based object environment, between you and the object, always only "one arm away", for example, two given handle H1 and H2, only when the corresponding assignment operator is defined for this type of handle, *H1 = *H2 works fine, but for types in languages other than C++/CLI, This may not be the case. Similarly, a CLS-compliant mechanism requires the creation of a copy of the object, which is called "cloning."
Using the clone functions in the CLI library
Consider the code in Example 1, which uses a system::arraylist class similar to a vector, and inserting 1 is the output of the program.
Example 1:
using namespace System;
using namespace System::Collections;
void PrintEntries(String^ s, ArrayList^ aList);
int main()
{
ArrayList^ al1 = gcnew ArrayList;
/*1*/ al1->Add("Red");
al1->Add("Blue");
al1->Add("Green");
al1->Add("Yellow");
/*2*/ PrintEntries("al1", al1);
/*3*/ ArrayList^ al2 = static_cast<ArrayList^>(al1->Clone());
/*4*/ PrintEntries("al2", al2);
/*5*/ al1->Remove("Blue");
al1->Add("Black");
al1->RemoveAt(0);
al1->Insert(0, "Brown");
/*6*/ PrintEntries("al1", al1);
/*7*/ PrintEntries("al2", al2);
}
void PrintEntries(String^ s, ArrayList^ aList)
{
Console::Write("{0}: ", s);
for each(Object^ o in aList)
{
Console::Write("\t{0}", o);
}
Console::WriteLine();
}