posts - 2,comments - 0,trackbacks - 0

 

 1// for_each_generics.cpp
 2// compile with: /clr
 3using namespace System;
 4using namespace System::Collections::Generic;
 5
 6generic <class T>
 7public value struct MyArray : public IEnumerable<T> {   
 8
 9   MyArray( array<T>^ d ) {
10      data = d;
11   }

12
13   ref struct enumerator : IEnumerator<T> {
14      enumerator( MyArray^ myArr ) {
15         colInst = myArr;
16         currentIndex = -1;
17      }

18
19      virtual bool MoveNext() = IEnumerator<T>::MoveNext {
20         if ( currentIndex < colInst->data->Length - 1 ) {
21            currentIndex++;
22            return true;
23         }

24
25         return false;
26      }

27
28      virtual property T Current {
29         T get() {
30            return colInst->data[currentIndex];
31         }

32      }
;
33
34      property Object^ CurrentNonGeneric {
35         virtual Object^ get() = System::Collections::IEnumerator::Current::get {
36            return colInst->data[currentIndex];
37         }

38      }
;
39
40      virtual void Reset() {}
41      ~enumerator() {}
42
43      MyArray^ colInst;
44      int currentIndex;
45   }
;
46
47   array<T>^ data;
48
49   virtual IEnumerator<T>^ GetEnumerator() {
50      return gcnew enumerator(*this);
51   }

52   virtual System::Collections::IEnumerator^ GetEnumeratorNonGeneric() = System::Collections::IEnumerable::GetEnumerator {
53      return gcnew enumerator(*this);
54   }

55}
;
56
57int main() {
58   MyArray<int> col = MyArray<int>( gcnew array<int>{102030 } );
59
60   for each ( Object^ c in col )
61      Console::WriteLine((int)c);
62}
 
63
posted on 2007-07-16 23:42 孙明明 阅读(195) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理