2. About Qing Education Ph.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
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 中的列舉型別
21. 不同的兩種物件生成及運用模式 *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;; }
25. 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); 執行結果:
30. 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() 被叫用兩次
38. Refernece Type 允許單一繼承多重實作 ref class R abstract {}; public ref class R2 : R, IClone, IComparable, IDisposable, IEnumerable { }; 所有 reference type 都繼承自 System::Object 最多繼承一個類別,但可以實作多個介面
42. 編譯模式之間的關連性 Native CLR Code Data Machine Code CLR Data / Types Native Data / Types MSIL Code Mixed C++ /clr Native C++ Verifiable C++ /clr:safe Pure C++ /clr:pure
44. 決定 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”
46. Tempalte 採用 Lazy Constraint (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 採用 Lazy Constraint (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
55. Template vs. Generics Templates 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