Each time we assign the same Object with a value type, a new boxing of the value occurs.
Allowing access to the boxed value type allows in-memory update, which may provide significant
performance ...
當我們每次分配同一的實值型別對象時,都會發生一次裝箱操作。允許訪問已裝箱的實值型別在記憶體中,將能顯著的提高程式效能。
A form of deep-copy semantics associated with the C++ copy constructor and copy assignment
operator; however, this could not be extended to value types.
將深拷貝文法關聯到C++的拷貝建構函式和拷貝操作符;但是沒有擴充到實值型別。
The CLI has no notion of the class destructor for a reference type. So the destructor has to be
mapped into something else in the underlying implementation。
CLI中對參考型別沒有解構函式的概念。因此在實現的時候解構函式不得不映射到其他函數上。
We still need a way to automate the invocation of the
destructor. A special stack-based notation for a reference type is supported; that is, one in which its
lifetime is associated within the scope of its declaration. Internally, the compiler transforms the
notation to allocate the reference object on the managed heap. With the termination of the scope,
the compiler inserts an invocation of the Dispose() method—the user-defined destructor. Reclamation
of the actual memory associated with the object remains under the control of the garbage collector.
我們仍然需要一種方法去自動調用解構函式。對於參考型別提供了一個特殊的棧標誌,對象的生命週期被關聯到它聲明的程式碼片段中。
普遍的編譯器根據這個標誌分配一個引用對象在託管堆上。在程式碼片段結束時,編譯器插入一個Dispose()調用-使用者定義的解構函式。
回收該對象關聯的記憶體仍然處於垃圾收集器的控制下。
ref class Wrapper {
Native *pn;
public:
// 構造即初始化
Wrapper( int val ) { pn = new Native( val ); }
// 自己釋放非託管記憶體
~Wrapper(){ delete pn; }
void mfunc();
protected:
// 明確聲明Finalize方法確保記憶體會被釋放
! Wrapper() { delete pn; }
};
void f1()
{
// 使用正常的參考型別
Wrapper^ w1 = gcnew Wrapper( 1024 );
// 映射參考型別到生命週期
Wrapper w2( 2048 );
// 比較調用文法的不同
w1->mfunc(); w2.mfunc();
// w2 在這裡被Dispose()釋放
}
//
// ... 然後, w1 可能在這裡被Finalize()釋放
C++/CLI編譯四種模式
1.Mixed mode: source-level mix of native and CTS types plus binary mix of native and CIL
object files. (Compiler switch: \clr.)
混合模式:將原生的型別與CTS型別在代碼級和二進位檔案級進行混合,通過\clr參數
2.Pure mode: source-level mix of native and CTS types. All compiled to CIL object files.
(Compiler switch: \clr:pure.)
純模式:將原生的型別與CTS型別在代碼級混合,並編譯所有檔案到CIL對象檔案,通過\clr:pure參數
3.Native class can hold CTS types through a special wrapper class only
原生類只能通過一個特殊封裝類來處理CTS型別
4.CTS classes can hold native types only as pointers
CTS類能夠處理的原生型別只有指標類型。
C++/CLI的組成
1.Assemblies(裝配器)
裝配器是構建.Net分布式應用程式的核心。裝配器是一個自我描述的集合儲存於中繼語言中,並負責分配應用程式需要的資源。裝配件由四個部分組成:裝配件中繼資料,類型中繼資料,微軟中繼語言代碼和資源。
Private assemblies reside in the same directory as the application itself or in one of its child directories. Shared assemblies, on the other hand, are stored in the global assembly cache (GAC).
私人裝配器位於應用程式同一目錄或是它的子目錄。共用裝配器儲存在全域裝配件緩衝中(GAC)。
可以通過反射技術獲得裝配器中的中繼資料資訊。
2.Common Language Runtime(統一語言運行環境)
as managed objects in .NET do not have a fixed location,but you can overcome this with the pin_ptr<> keyword.
託管對象在.NET中沒有固定的地址,不過你可以使用pin_ptr<>關鍵字來控制它。
The CLS is a minimum subset of the CTS that all languages must support to be.
CLS是CTS的最小子集,所有的.NET語言都需要支援它.
• Global methods and variables are not allowed.
不允許使用全域函數和變數
• The CLS does not impose case sensitivity, so make sure that all exposed types differ by more than their case.
CLS不區分大小寫
• The only primitive types allowed are Byte, Int16, Int32, Int64, Single, Double, Boolean, Char, Decimal, IntPtr, and String.
CLS只包含以上的幾種簡單的資料類型
• Variable-length argument lists are not allowed. Use fixed-length arrays instead.
使用固定的數組來代替可變參數列表
• Pointers are not allowed.
不允許指標
• Class types must inherit from a CLS-compliant class. System::Object is CLS compliant.
類類型必須繼承自CLS體系的類(這就是為什麼所有.NET類都繼承值System::Object的原因)
• Array elements must be CLS compliant.
數組元素必須是CLS體系中的元素