#眉標= C++ #副標= C++/CLI全景體驗 #大標= CLI陣列與列舉 #作者=文/Stanley Lippman•譯者/李建忠 ============程式============ 程式1 namespace cli { template ref class array : Array { }; } ============程式============ ============程式============ array^ words; ============程式============ ============程式============ array^ words = gcnew array( 1024 ); ============程式============ ============程式============ array^ 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^ words = gcnew array( 8 ) { "piglet", "pooh", "eyeore" }; ============程式============ ============程式============ array^ Fibonacci = gcnew array() { 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 }; ============程式============ ============程式============ array ^wordlist; ============程式============ ============程式============ wordlist = gcnew array() { "a", "t", "g", "c" }; wordlist = gcnew array() { "tac", "gcg", "aat", "ttt", "atc" }; ============程式============ ============程式============ 程式2 // 一維陣列 array^ myArray = gcnew array( 4 ); // 二維陣列 array^ myMat = gcnew array( 4,4 ) {{ "a", "b", "c" }{ "e", "f" }}; // 三維陣列 array ^myMy = gcnew array( 2,3,4 ); ============程式============ ============程式============ 程式3 // 方法聲明 array^ linesOfText( String^ filename ); // 方法呼叫 array^ textlines = linesOfText( "c:\\C++_CLI Primer" ); ============程式============ ============程式============ 程式4 // 隱式判斷tracking handle是否有效 if ( ! textlines ) … 或者 // 顯式判斷tracking handle是否有效 if (textlines == nullptr ) … ============程式============ ============程式============ 程式5 ??? separateWords( array^ textlines ) { if ( ! textlines ) return; // … } // 可能的一種呼叫 separateWords( linesOfText( "c:\\C++_CLI Primer" )); ============程式============ ============程式============ array^>^ fullText; ============程式============ ============程式============ fullText = gcnew array^>( textlines->Length ); ============程式============ ============程式============ 程式6 String^ line; for( int ix = 0; ix < textlines->Length; ++ix ) { line = textlines[ ix ]; fullText[ ix ] = line->Split( nullptr ); } ============程式============ ============程式============ 程式7 array^ line; for( int ix = 0; ix < fullText->Length; ++ix ) { line = fullText[ ix ]; // … do something } 也可以使用更精簡的for each語句: for each ( array^ as in fullText ) // … do something ============程式============ ============程式============ 程式8 array^>^ separateWords( array^ 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^ args ); ============程式============ ============程式============ 程式10 public ref class Messager { public: // … void message( String^ ); void message( String^, … array^ ); void message( String^, … array^ ); void message( String^, … array^ ); }; ============程式============ ============程式============ 程式11 void message( Messager ^mh ) { int ival= 10, dval = 0; // 呼叫 message( String^ ); mh->message( "just a string literal" ); // 呼叫 messages( String^, … array^ ); 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^ ); ============程式============ ============程式============ mh->message( "weird types", false, mh, 3.14, "literal" ); ============程式============ ============程式============ void message( String^, … array^ ); ============程式============ ============程式============ 程式12 void Messager:: message( String^ msg, … array^ 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^ args ); // 接受0或多個任意型別的參數 void func2( … array^ args ); ============程式============ ============程式============ 程式14 void display( String^ ); void display( String^, int, … array^ ); 以及下面的呼叫: display( "message", 42, arg1, arg2 ); ============程式============ ============程式============ void f(...); // (1) void f( … array^ ); // (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 // 類別 ============程式============