^ (Handle to Object on Managed Heap)
来源:互联网 发布:淘宝怎样抢购 编辑:程序博客网 时间:2024/05/29 18:58
Declares a handle to an object on the managed heap.
Remarks
A handle to an object on the managed heap points to the "whole" object, and not to a member of the object.
See gcnew for information on how to create an object on the managed heap.
In Visual C++ 2002 and Visual C++ 2003, __gc * was used to declare an object on the managed heap. The ^ replaces __gc * in the new syntax.
The common language runtime maintains a separate heap on which it implements a precise, asynchronous, compacting garbage collection scheme. To work correctly, it must track all storage locations that can point into this heap at runtime. ^ provides a handle through which the garbage collector can track a reference to an object on the managed heap, thereby being able to update it whenever that object is moved.
Because regular C++ pointers (*) and references (&) cannot be tracked precisely, a handle-to object declarator is used.
Member selection through a handle (^) uses the pointer-to-member operator (->).
For more information, see
Example
This sample shows how to create an instance of reference type on the managed heap. This sample also shows that you can initialize one handle with another, resulting in two references to same object on managed, garbage-collected heap. Notice that assigning nullptr to one handle does not mark the object for garbage collection.
// mcppv2_handle.cpp// compile with: /clrref class MyClass {public: MyClass() : i(){} int i; void Test() { i++; System::Console::WriteLine(i); }};int main() { MyClass ^ p_MyClass = gcnew MyClass; p_MyClass->Test(); MyClass ^ p_MyClass2; p_MyClass2 = p_MyClass; p_MyClass = nullptr; p_MyClass2->Test(); }
Output
12
The following sample shows how to declare a handle to an object on the managed heap, where the type of object is a boxed value type. The sample also shows how to get the value type from the boxed object.
// mcppv2_handle_2.cpp// compile with: /clrusing namespace System;void Test(Object^ o) { Int32^ i = dynamic_cast<Int32^>(o); if(i) Console::WriteLine(i); else Console::WriteLine("Not a boxed int");}int main() { String^ str = "test"; Test(str); int n = 100; Test(n);}
Output
Not a boxed int100
This sample shows that the common C++ idiom of using a void* pointer to point to an arbitrary object is replaced by Object^, which can hold a handle to any reference class. It also shows that all types, such as arrays and delegates, can be converted to an object handle.
// mcppv2_handle_3.cpp// compile with: /clrusing namespace System;using namespace System::Collections;public delegate void MyDel();ref class MyClass {public: void Test() {}};void Test(Object ^ x) { Console::WriteLine("Type is {0}", x->GetType());}int main() { // handle to Object can hold any ref type Object ^ h_MyClass = gcnew MyClass; ArrayList ^ arr = gcnew ArrayList(); arr->Add(gcnew MyClass); h_MyClass = dynamic_cast<MyClass ^>(arr[0]); Test(arr); Int32 ^ bi = 1; Test(bi); MyClass ^ h_MyClass2 = gcnew MyClass; MyDel^ DelInst = gcnew MyDel(h_MyClass2, &MyClass::Test); Test(DelInst);}
Output
Type is System.Collections.ArrayListType is System.Int32Type is MyDel
This sample shows that a handle can be dereferenced and that a member can be accessed via a dereferenced handle.
// mcppv2_handle_4.cpp// compile with: /clrusing namespace System;value struct DataCollection {private: int Size; array<String^>^ x;public: DataCollection(int i) : Size(i) { x = gcnew array<String^>(Size); for (int i = 0 ; i < Size ; i++) x[i] = i.ToString(); } void f(int Item) { if (Item >= Size) { System::Console::WriteLine("Cannot access array element {0}, size is {1}", Item, Size); return; } else System::Console::WriteLine("Array value: {0}", x[Item]); }};void f(DataCollection y, int Item) { y.f(Item);}int main() { DataCollection ^ a = gcnew DataCollection(10); f(*a, 7); // dereference a handle, return handle's object (*a).f(11); // access member via dereferenced handle}
Output
Array value: 7Cannot access array element 11, size is 10
This sample shows that a native reference (&) can’t bind to an int member of a managed type, .as the int may be stored in the garbage collected heap, and native references don’t track object movement in the managed heap. The fix is to use a local variable, or to change & to %, making it a tracking reference.
// mcppv2_handle_5.cpp// compile with: /clrref struct A { void Test(unsigned int &){} void Test2(unsigned int %){} unsigned int i;};int main() { A a; a.i = 9; a.Test(a.i); // C2664 a.Test2(a.i); // OK unsigned int j = 0; a.Test(j); // OK}
- ^ (Handle to Object on Managed Heap)
- How to handle exception in managed code and unmanaged code
- How to handle exception in managed code and unmanaged code
- C#-value type array allocated on managed Heap.
- How to RESOLVE a breakpoint on a managed Main method.
- Attach to an already running managed process on device
- Tips to handle Msxml2.XMLHTTP object @ Asp.Net
- How to handle chevron button using CToolBarCtrl object?
- Effecitive C++ 28. Avoid returning "handle" to object internals
- How to handle the concurrency problems on ASP.Net Database
- GDI Object and handle
- Object(String,list,object and so on) To Json
- How to: Access the HTML Source in the Managed HTML Document Object Model
- How to set a breakpoint on a property within a third-party managed component?
- Configuring DebugDiag to Automatically Capture a Full User Dump on a Managed Function
- How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?
- 深入.NET托管堆(managed heap)
- [转]深入.NET托管堆(managed heap)
- 采药问题(NOIP竞赛原题)
- 员工在一个企业内的心态变化发展过程
- 歌美X760个人使用感受
- 如何使用 ldconfig 及 LD_LIBRARY_PATH
- 试验网站#1搜索引擎优化收录情况记录(断续运行)2007-10-16
- ^ (Handle to Object on Managed Heap)
- Linux shell I/O重定向详解
- 试验网站#3搜索引擎优化收录情况记录2007-10-16
- PHP分页完整
- 一个HTML分离类
- Delphi中的消息钩子函数和Windows子类处理 入门篇
- 最近解决的一个内存泄漏问题
- java入门必看的30个问题
- PHP,JS 一些常用的正则表达式