Instance parsing c++/cli "clones"

Source: Internet
Author: User

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();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.