#眉標= C++ #副標= C++/CLI全景體驗 #大標= 認識C++/CLI Managed #作者=文/Stanley Lippman‧譯者/李建忠 #引言= -----box----- #程式1 using namespace System; int main() { String^ msg =gcnew String("Hello, World!"); Console::WriteLine(text); } -----end----- -----程式----- cl /clr hello_world.cpp -----程式----- -----程式----- String^ msg ="Hello, World!"; -----程式----- -----程式----- StreamReader^ sr = gcnew StreamReader("note.txt"); … delete sr; -----程式----- -----程式----- String^ msg … -----程式----- -----box----- #程式2 int index; // 32位整數, 內建數值型別 value struct Point { // 自定義數值型別 int x; int y; }; enum class Color { Red, Green, Blue }; // 列舉型別 -----end----- -----box----- #程式3 Object^ o=nullptr; // Object, 內建參考型別 String^ s="Hello, C++/CLI"; // String, 內建參考型別 interface class IShape { // 接口型別 void Draw(); }; ref struct Circle: IShape { // 自定義參考型別 Point centre; int radius; void Draw(){…} }; delegate void MyDelegate(); // 委託型別 int^ hint=gcnew int(100); // 裝箱數值型別 array<int>^ a=gcnew array<int>(10); // 數組型別 Circle^ c=gcnew Circle(); MyDelegate^ d=gcnew MyDelegate(c,&Circle::Draw); interior_ptr<int> p=&c->radius ; // 指標型別 -----end----- -----程式----- [Serializable] ref struct Point{ int x; int y;}; -----程式----- -----box----- #程式4 #include <iostream> #include <string> using namespace std; int main() { string msg="Hello, World!"; cout<<msg; } 這段程式碼同樣可以用cl /clr來編譯: cl /clr nativeHello.cpp -----end----- -----box----- #程式5 #include "TextQuery.h" public ref class TextQueryCLI { TextQuery *pquery; public: TextQueryNet() : pquery( new TextQuery()){} ~TextQueryNet(){ delete pquery; } void query_text() { pquery->query_text(); } void build_up_text() { pquery->build_up_text();} // … }; -----end-----