VC.net继承IEnumerator接口的问题

北京的雾霾天 2012-12-19 01:18:35
有如下代码:
		ref class Enumerator : System::Collections::Generic::IEnumerator<ItemType>, System::IDisposable
{
private:
ItemCollection<ItemType>^ m_List;
System::Int32 m_Index;
System::Int32 m_Version;
ItemType m_Current;
public:
Enumerator(ItemCollection<ItemType>^ list)
{
this->m_List=list;
this->m_Index=0;
this->m_Version=list->m_Version;
this->m_Current=ItemType();
}
virtual System::Boolean MoveNext()
{
ItemCollection<ItemType>^ list = this->m_List;
if ((this->m_Version == list->m_Version) && (this->m_Index < list->m_Size))
{
this->m_Current = list->m_InnerArray[this->m_Index];
this->m_Index++;
return true;
}
if (this->m_Version != list->m_Version)
{
throw gcnew System::InvalidOperationException("集合已改变,操作无效");
}
this->m_Index = this->m_List->m_Size + 1;
this->m_Current = ItemType();
return false;
}
//1
property virtual System::Object^ Current
{
System::Object^ get(){return this->m_Current;}
}
property virtual ItemType Current
{
ItemType get()
{
return this->m_Current;
}
}
virtual void Reset()
{
this->m_Index = 0;
this->m_Current = ItemType();
}
~Enumerator()
{
}

在标记1处的Current是不符合接口定义的,需要定义泛型的Current属性。

property virtual ItemType Current
{
ItemType get(){return this->m_Current;}
}

但是两个Current不能同时定义,怎么办?
VC.Net的显式重写并不能解决属性的问题,那该如何实现这个自定义的Class?
...全文
341 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京的雾霾天 2012-12-19
  • 打赏
  • 举报
回复
OK! 就是我想要的! 我没有找到这个示例,所以非常感谢!
真相重于对错 2012-12-19
  • 打赏
  • 举报
回复
给你找个msdn的代码
// for_each_generics.cpp
// compile with: /clr
using namespace System;
using namespace System::Collections::Generic;

generic <class T>
public value struct MyArray : public IEnumerable<T> {   

   MyArray( array<T>^ d ) {
      data = d;
   }

   ref struct enumerator : IEnumerator<T> {
      enumerator( MyArray^ myArr ) {
         colInst = myArr;
         currentIndex = -1;
      }

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

         return false;
      }

      virtual property T Current {
         T get() {
            return colInst->data[currentIndex];
         }
      };

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

      virtual void Reset() {}
      ~enumerator() {}

      MyArray^ colInst;
      int currentIndex;
   };

   array<T>^ data;

   virtual IEnumerator<T>^ GetEnumerator() {
      return gcnew enumerator(*this);
   }
   virtual System::Collections::IEnumerator^ GetEnumeratorNonGeneric() = System::Collections::IEnumerable::GetEnumerator {
      return gcnew enumerator(*this);
   }
};

int main() {
   MyArray<int> col = MyArray<int>( gcnew array<int>{10, 20, 30 } );

   for each ( Object^ c in col )
      Console::WriteLine((int)c);
}
真相重于对错 2012-12-19
  • 打赏
  • 举报
回复
你这个不符合c++/clr语法
北京的雾霾天 2012-12-19
  • 打赏
  • 举报
回复
主要是同名属性的问题,属性名称相同,但是类型不一样。 在C#里通过显示实现接口就行了,但是在VC.Net语法都通不过。
  • 打赏
  • 举报
回复
没怎么看明白- - ,帮顶了。

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧