c++-cli - C++/CLI IEnumerable 和 IEnumerator 实现

标签 c++-cli ienumerable ienumerator

有没有人有一个关于如何在 C++/CLI 中实现 IEnumerable 和 IEnumerator 的工作的分步示例?或者,是否有人知道如何从 MS Connect 修复以下无法在 Visual Studio 2005 中编译的代码?

http://connect.microsoft.com/VisualStudio/feedback/details/101089/how-to-implement-ienumerable-t-and-ienumerable-c-cli

using namespace System;
using namespace System::Collections::Generic;

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

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

        bool MoveNext()
        {
            if( currentIndex < colInst->data->Length - 1 )
            {
                currentIndex++;
                return true;
            }
            return false;
        }

        property T Current
        {
            T get()
            {
                return colInst->data[currentIndex];
            }
        };
        // This is required as IEnumerator<T> also implements IEnumerator
        property Object^ Current2
        {
            virtual Object^ get() new sealed = System::Collections::IEnumerator::Current::get
            {
                return colInst->data[currentIndex];
            }
        };

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

        MyArray^ colInst;
        int currentIndex;
    };

    array<T>^ data;

    IEnumerator<T>^ GetEnumerator()
    {
        return gcnew enumerator(this);
    }

    virtual System::Collections::IEnumerator^ GetEnumerator2() new sealed = System::Collections::IEnumerable::GetEnumerator
    {
        return gcnew enumerator(this);
    }
};

int main()
{
    int retval = 0;

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

    for each( Object^ c in col )
    {
        retval += (int)c;
    }
    retval -= 10 + 20 + 30;

    Console::WriteLine("Return Code: {0}", retval );
    return retval;
}

编译器无法找到枚举器方法实现:
error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'bool System::Collections::IEnumerator::MoveNext(void)'   c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  55  

error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'void System::Collections::IEnumerator::Reset(void)'  c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  55  

error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'T System::Collections::Generic::IEnumerator<T>::Current::get(void)'  c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  55  

error C3766: 'MyArray<T>' must provide an implementation for the interface method 'System::Collections::Generic::IEnumerator<T> ^System::Collections::Generic::IEnumerable<T>::GetEnumerator(void)' c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp  68  

最佳答案

这为我编译,没有任何警告(在 VS2010 上):

using namespace System;
using namespace System::Collections::Generic;

generic <class T>
public ref 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;
        }

        property T Current
        {
            virtual T get() = IEnumerator<T>::Current::get
            {
                return colInst->data[currentIndex];
            }
        };
        // This is required as IEnumerator<T> also implements IEnumerator
        property Object^ Current2
        {
            virtual Object^ get() = System::Collections::IEnumerator::Current::get
            {
                return colInst->data[currentIndex];
            }
        };

        virtual void Reset() = IEnumerator<T>::Reset {}
        ~enumerator() {}

        MyArray^ colInst;
        int currentIndex;
    };

    array<T>^ data;

    virtual IEnumerator<T>^ GetEnumerator()
    {
        return gcnew enumerator(this);
    }

    virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
    {
        return gcnew enumerator(this);
    }
};

int main()
{
    int retval = 0;

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

    for each( Object^ c in col )
    {
        retval += (int)c;
    }
    retval -= 10 + 20 + 30;

    Console::WriteLine("Return Code: {0}", retval );
    return retval;
}

关于c++-cli - C++/CLI IEnumerable 和 IEnumerator 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086098/

相关文章:

wpf - 混合 MFC 和 WPF : Modal Dialogs

c++ - C++ 项目中 C++/CLI dll Unresolved 符号错误

c# - 带有 Win32 父窗口的 WPF OpenFileDialog;窗口在关闭时没有重新获得焦点

c# - 在从头构建的链表上用 C# 实现 IEnumerable<T>

c# - 为什么 IEnumerator<T> 继承自 IDisposable 而非泛型 IEnumerator 却没有?

c# - 实现集合的简单方法?

.net - 在 Windows 窗体应用程序中使用 C++/CLI 的命名空间混淆

c# - 将 System.Collections.Generic.SortedList<string,T> 转换为 IEnumerable<T>

c# - 绑定(bind)到数据库的自定义通用 IEnumerator

c# - Yield Return == IEnumerable 和 IEnumerator 是吗?