Recently I got a couple of friends asking what is a strongly typed boxed instance of a value type in C++/CLI webcast, Visual C + +?
The lecture was in a hurry, so the technical point was just a little bit, not in detail. Here borrow the blog to expand it to say.
First look at the following C # code:
using System;
using System.Collections;
struct MyClass
{
public int data;
}
class Test
{
public static void Main()
{
MyClass myClass1 = new MyClass();
MyClass myClass2=new MyClass();
ArrayList list=new ArrayList();
list.Add(myClass1);
list.Add(myClass2);
Print(list);
for(int i=0;i<list.Count;i++)
{
MyClass temp=(MyClass)list[i];
temp.data=i+1;
list[i]=temp;// 注意这句话
}
Print(list);
}
public static void Print(ArrayList list)
{
for(int i=0;i<list.Count;i++)
{
MyClass temp=(MyClass)list[i];
Console.WriteLine(temp.data);
}
}
}
It is important to list[i]=temp that the change cannot be implemented because temp is an instance of a "stack that is not related to the value instance of a hold boxed in the list."
But if you use C++/CLI, we can do this:
using namespace System;
using namespace System::Collections;
value class MyClass {
public:
int data;
};
void Print(ArrayList^ list);
int main() {
MyClass^ hMyClass1 = gcnew MyClass;
MyClass^ hMyClass2 = gcnew MyClass;
ArrayList^ list=gcnew ArrayList();
list->Add(hMyClass1);
list->Add(hMyClass2);
Print(list);
for(int i=0;i<list->Count;i++)
{
MyClass^ temp=(MyClass^)list[i];
temp->data=i+1; // 注意这里无需再有list[i]=temp这句话,因为temp引用的就是“list中hold的boxed的值实例”
}
Print(list);
}
void Print(ArrayList^ list)
{
for(int i=0;i<list->Count;i++)
{
MyClass^ temp=(MyClass^)list[i];
Console::WriteLine(temp->data);
}
}
In fact in C #, we use MyClass temp= (MyClass) list[i]; A unbox and a copy action occurred in the middle.
And in C++/cli, we use myclass^ temp= (myclass^) list[i]; There is only unbox in the middle, and no copy action occurs--note that only the unbox is occurring and the data is in the managed heap, but the type is myclass^ so that we can get directly to the MyClass data member--that's what I call " Strongly-typed boxed instances of value types.
This is not possible in C #, only a "weak type" with type Object--one way is to use interface to implement indirectly, because interface is a reference type.
A strongly typed boxed instance of a value type certainly does more than just mean "write less code," which in fact provides a good performance boost-because the unbox cost is very small and is separate from the copy action. Unlike box, it is a costly operation because it contains the copy action itself.
In fact, the powerful description of the type of C++/CLI is not only reflected in this place, there are many, such as interior_ptr, this will be my future will be analyzed c++/cli a focus, but also I wrote last year, "Casual talk about the c++/cli in several pointers and references (1)" (http ://blog.dreambrook.com/jzli/archive/2004/11/20/352.aspx) Series of planning-unfortunately later due to too busy, no concentrated time to start. It takes a considerable amount of space to discuss it, because it involves an in-depth analysis of the managed object model. Below I will expand this series of blogs with my own analysis of the shared source CLI (rotor) source code.