About Qing EducationPh.D. Candidate, Department of Computer Science, National Tsing-Hua University, Taiwan Research interests: distribute network management, mobile agent, VoIP, and p2p networking Software Development Skills Programming languages: 80x86 assembly, C/C++, Java, C# J2EE development and Web programming: EJB, JSP/Servlet Network programming: TCP/IP, socket programming Object Oriented Design/Programming Design Patterns and Software Architecture Distributed Network Management System Peer-to-Peer Networking Book Translation Thinking in Java 2nd Edition, in Traditional Chinese Essential C++, in Traditional Chinese
C++ 及 CLI物件模型的比較 物件儲存空間類型多樣化 static, stack, 以及 heap 物件的生命期類型多樣化 static object 有著和程式相同的生命期 stack 上的 object ,則在和 scope({ … }) 的生命期相同 heap 上的 object ,則由程式員自行操控其生命期 物件建構及指派的深層拷貝模型 編譯時,原始碼中必須含括所有動用型別的資訊 物件儲存空間類型單純 Value type 儲存於 stack Reference type 儲存於 managed heap 上 物件的生命期具不確定性 Reference types 須倚靠垃圾收集器回收 物件的指派採淺層參考語義 編譯時,型別資訊由所使用型別的 metadata 提供 C++ 的靜態物件模型 CLI 的動態物件模型
14.
C++/CLI 中的型別宣告 在C++/CLI 中可宣告下述型別 CLR 的 enumeration type enum class E {…} CLR 的 interface type interface class I {…} CLR 的 value type value class V {…} CLR 的 reference type ref class R {…} C++ 原生類別(過去 C++ 程式員所用) class N {…} 所宣告的型別 宣告語法
15.
C++ 與 CTS型別的對應 N/A Boolean bool N/A Double long double N/A Double double N/A Single float UInt64 Int64 __int64 UInt32 Int32 long int UInt32 Int32 int, __int32 UInt16 Int16 short int Byte Sbyte char CTS Unsigned Type CTS Signed Type C++ Type
16.
各型別的用途 原生型別 具備native code 的語義及優點 即使被編譯成 MSIL 亦如此 Reference Type 可被垃圾收集器回收,提供簡便的記憶體管理模式 Value Type 輕量級的型別(例如像整數之類的型別) Interface Type 宣告 CLR 中的介面型別 Enumeration Type 宣告 CLR 中的列舉型別
不同的兩種物件生成及運用模式 *Kate Gregory,“Moving C++ Applications to the Common Language Runtime” Use Native Heap Use Managed Heap Use Stack ^ and % always * and & never Verifiability d elete delete Free gcnew new Allocate % & Reference ^ * Pointer / Handle Managed Native
22.
於 Native/CLI 型別中混用Pointer/Handle (1/2) 在 CLI 型別中使用 Handle 在 CLI 型別中使用 Pointer ref class R { private: String ^str; } ref class R { private: std::string* str; }
23.
於 Native/CLI 型別中混用Pointer/Handle (2/2) 在 Native 型別中使用 Handle gcroot<T> 是用來包裝 System.Runtime.InteropServices.GCHandle 的一個 template class *http://www.codeproject.com/managedcpp/ijw_unmanaged.asp class N { private: gcroot<String^> str;; }
Tracking Reference 的效應array<String^>^ arr = gcnew array<String^>(3); int i = 0; for each(String^% s in arr) s = gcnew String(i++.ToString()); for each(String^ s in arr) Console::WriteLine(s); 執行結果: 0 1 2 array<String^>^ arr = gcnew array<String^>(3); int i = 0; for each(String^ s in arr) s = gcnew String(i++.ToString()); for each(String^ s in arr) Console::WriteLine(s); 執行結果:
Why SuppressFinalize? public class FileStream : Stream { public override void Close() { // Clean up this object: flush data and close file … // There is no reason to Finalize this object now GC.SuppressFinalize(this); } protected override void Finalize() { Close(); // Clean up this object: flush data and close file } // Rest of FileStream methods go here … } *http://www.codeproject.com/managedcpp/cppclidtors.asp 如果程式員自行呼叫了 Close() , 但 GC 又呼叫了 Finalize() 便會引發 Close() 被叫用兩次
Refernece Type 允許單一繼承多重實作ref class R abstract {}; public ref class R2 : R, IClone, IComparable, IDisposable, IEnumerable { }; 所有 reference type 都繼承自 System::Object 最多繼承一個類別,但可以實作多個介面
編譯模式之間的關連性 Native CLRCode Data Machine Code CLR Data / Types Native Data / Types MSIL Code Mixed C++ /clr Native C++ Verifiable C++ /clr:safe Pure C++ /clr:pure
決定 Managed/Unmanaged 的交界處C++ managed C++ CRT, STL, etc One call to foo() Hundreds of calls C# or C++ managed C++ CRT, STL, etc One call to foo() Hundreds of calls C# C++ CRT, STL, etc Hundreds of calls C++ One call to foo() One call * Kate Gregory, “ Moving C++ Applications to the Common Language Runtime”
Tempalte 採用 LazyConstraint (1/2) template<typename T> class Native { public: void Start(int x) { T* t = new T(); t->Bark(x); t->WagTail(); delete t; } }; 如何確定 t 有 Bark() 及 WagTail() 兩 methods 呢? *http://www.codeproject.com/managedcpp/cppcligenerics.asp
47.
Tempalte 採用 LazyConstraint (2/2) Native<NativeDog> d1; d1.Start(100); Native<NativePig> d2; d2.Start(100); 引發編譯器錯誤: error C2039: 'Bark' : is not a member of 'NativePig' *http://www.codeproject.com/managedcpp/cppcligenerics.asp
Template vs. GenericsTemplates are instantiated at compile-time with the source code. The type checking of a template are performed at the point where the template is defined and instantiated . Tempaltes allow specialization. Templates allow non-type parameters. Templates use " lazy structural constraints ". Generics are instantiated at run-time by the CLR. The type checking of a generic is peformend at the point where the generic is defined . Generics are cross-language . Generics do not allow specialization. Generics do not allow non-type parameters. Generics use subtype constraints . *http://blogs.msdn.com/branbray/archive/2003/11/19/51023.aspx