#眉標= C++ #副標= C++/CLI全景體驗 #大標= CLI陣列與列舉 #作者=文/Stanley Lippman‧譯者/李建忠 ============程式============ 程式1 namespace cli { template<typename T, int rank = 1> ref class array : Array { }; } ============程式============ ============程式============ array<String^>^ words; ============程式============ ============程式============ array<String^>^ words = gcnew array<String^>( 1024 ); ============程式============ ============程式============ array<String^>^ words = { "piglet", "pooh", "eyeore" }; ============程式============ ============程式============ if ( words->Length != 3 ) … ============程式============ ============程式============ for ( int ix = 0; ix < words->Length; ++ix ) Console::WriteLine( "{0}: {1}", ix, words[ ix ] ); ============程式============ ============程式============ for each( String^ w in words ) Console::WriteLine( w ); ============程式============ ============程式============ array<String^>^ words = gcnew array<String^>( 8 ) { "piglet", "pooh", "eyeore" }; ============程式============ ============程式============ array<int>^ Fibonacci = gcnew array<int>() { 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 }; ============程式============ ============程式============ array<String^> ^wordlist; ============程式============ ============程式============ wordlist = gcnew array<String^>() { "a", "t", "g", "c" }; wordlist = gcnew array<String^>() { "tac", "gcg", "aat", "ttt", "atc" }; ============程式============ ============程式============ 程式2 // 一維陣列 array<Object^>^ myArray = gcnew array<Object^>( 4 ); // 二維陣列 array<String^,2>^ myMat = gcnew array<String^,2>( 4,4 ) {{ "a", "b", "c" }{ "e", "f" }}; // 三維陣列 array<int,3> ^myMy = gcnew array<int,3>( 2,3,4 ); ============程式============ ============程式============ 程式3 // 方法聲明 array<String^>^ linesOfText( String^ filename ); // 方法呼叫 array<String^>^ textlines = linesOfText( "c:\\C++_CLI Primer" ); ============程式============ ============程式============ 程式4 // 隱式判斷tracking handle是否有效 if ( ! textlines ) … 或者 // 顯式判斷tracking handle是否有效 if (textlines == nullptr ) … ============程式============ ============程式============ 程式5 ??? separateWords( array<String^>^ textlines ) { if ( ! textlines ) return; // … } // 可能的一種呼叫 separateWords( linesOfText( "c:\\C++_CLI Primer" )); ============程式============ ============程式============ array<array<String>^>^ fullText; ============程式============ ============程式============ fullText = gcnew array<array<String^>^>( textlines->Length ); ============程式============ ============程式============ 程式6 String^ line; for( int ix = 0; ix < textlines->Length; ++ix ) { line = textlines[ ix ]; fullText[ ix ] = line->Split( nullptr ); } ============程式============ ============程式============ 程式7 array<String^>^ line; for( int ix = 0; ix < fullText->Length; ++ix ) { line = fullText[ ix ]; // … do something } 也可以使用更精簡的for each語句: for each ( array<String^>^ as in fullText ) // … do something ============程式============ ============程式============ 程式8 array<array<String^>^>^ separateWords( array<String^>^ textlines ) { // … } ============程式============ ============程式============ 程式9 public ref class Messager { void message( char ch ); // for newline, tab, space … void message( string msg ){…} void message( string msg, int val ){…} void message( string msg, int v1, int v2 ){…} // … }; ============程式============ ============程式============ void Trace1( String^ format, … array<Object^>^ args ); ============程式============ ============程式============ 程式10 public ref class Messager { public: // … void message( String^ ); void message( String^, … array<int>^ ); void message( String^, … array<double>^ ); void message( String^, … array<Object^>^ ); }; ============程式============ ============程式============ 程式11 void message( Messager ^mh ) { int ival= 10, dval = 0; // 呼叫 message( String^ ); mh->message( "just a string literal" ); // 呼叫 messages( String^, … array<int>^ ); mh->message( "mumble", 10, ival, dval, 1024 ); mh->message( "mumble", ival ); mh->message( "fib: ", 1,1,2,3,5,8,13,21,34,55 ); } ============程式============ ============程式============ void message( String^, … array<double>^ ); ============程式============ ============程式============ mh->message( "weird types", false, mh, 3.14, "literal" ); ============程式============ ============程式============ void message( String^, … array<Object^>^ ); ============程式============ ============程式============ 程式12 void Messager:: message( String^ msg, … array<Object^>^ args ); { Console::WriteLine( msg ); if ( ! args->Length ) return; for each ( Object^ o in args ) Console::WriteLine( "\t{0}", o->ToString() ); } ============程式============ ============程式============ 程式13 // 接受0或多個int型別的參數 void func1( … array<int>^ args ); // 接受0或多個任意型別的參數 void func2( … array<Object^>^ args ); ============程式============ ============程式============ 程式14 void display( String^ ); void display( String^, int, … array<Object^>^ ); 以及下面的呼叫: display( "message", 42, arg1, arg2 ); ============程式============ ============程式============ void f(...); // (1) void f( … array<int>^ ); // (2) ============程式============ ============程式============ foo( 1, 2 ); ============程式============ ============程式============ enum class status { fail, pass }; ============程式============ ============程式============ public enum class status { fail, pass }; ============程式============ ============程式============ public enum class status : char { fail, pass }; ============程式============ ============程式============ void print( Object^ ); void print( int ); ============程式============ ============程式============ class enum status { fail, pass } me = status::pass; enum weekend{ sat, sun } ne = sun; ============程式============ ============程式============ print( me ); // CLI列舉型別 ============程式============ ============程式============ print( ne ); // 原生列舉型別 ============程式============ ============程式============ void display( int ); ============程式============ ============程式============ display( me ); // 錯誤: 不存在標準轉換 ============程式============ ============程式============ display(( int ) me ); // ok … ============程式============ ============程式============ class enum status { fail, pass } me = status::pass; ============程式============ ============程式============ 程式15 System::AssemblyLoadEventHandler // 委託 System::PlatformID // 列舉 System::IDisposable // 介面 System::String // 類別 ============程式============