实现IEnumerable< T>在C ++ / CLI中 [英] Implementing IEnumerable<T> in C++/CLI

查看:100
本文介绍了实现IEnumerable< T>在C ++ / CLI中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ / CLI中的自定义集合类中实现 IEnumerable< T> 时遇到问题。这是代码的相关部分:

I'm having problems implementing IEnumerable<T> in my custom collection class in C++/CLI. Here is the relevant part of the code:

using namespace System::Collections::Generic;

ref class MyCollection : IEnumerable<MyClass^>
{
public:
    MyCollection()
    {
    }  

    virtual IEnumerator<MyClass^>^ GetEnumerator()
    {
        return nullptr;
    }
};

编译时,会导致以下错误:

When compiled, this results in the following errors:


错误C2392:
'System :: Collections :: Generic :: IEnumerator
^ MyCollection :: GetEnumerator(void)':
协变量返回类型在托管类型中不支持
,否则
'System :: Collections :: IEnumerator
^ System :: Collections :: IEnumerable :: GetEnumerator(void)'
将被覆盖错误C3766:
'MyCollection'必须为接口
方法提供
实现
方法
'System :: Collections :: IEnumerator
^ System :: Collections :: IEnumerable :: GetEnumerator(void)'

error C2392: 'System::Collections::Generic::IEnumerator ^MyCollection::GetEnumerator(void)': covariant returns types are not supported in managed types, otherwise 'System::Collections::IEnumerator ^System::Collections::IEnumerable::GetEnumerator(void)' would be overridden error C3766: 'MyCollection' must provide an implementation for the interface method 'System::Collections::IEnumerator ^System::Collections::IEnumerable::GetEnumerator(void)'

这很有意义,因为 IEnumerable< T> 源自 IEnumerable 。但是,我不确定如何解决此编译错误。如果这是C#,我将隐式实现 IEnumerable ,但是我不确定如何在C ++ / CLI中做到这一点(如可能):

This makes sense, since IEnumerable<T> derives from IEnumerable. However, I'm not sure how to fix this compile error. If this was C#, I would implicitly implement IEnumerable, however I'm not sure how to do that in C++/CLI (if that's even possible) like this:

class MyCollection : IEnumerable<MyClass>
{
    public MyCollection()
    {
    }

    public IEnumerator<MyClass> GetEnumerator()
    {
        return null;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

如果我添加了<$ c $的实现c> IEnumerable :: GetEnumerator(),编译器抱怨两个方法仅在返回类型上有所不同(这也很有意义)。

If I do add an implementation of IEnumerable::GetEnumerator(), the compiler complains about two methods that differ only by return type (which also makes sense).

那么,如何在C ++ / CLI类中实现 IEnumerable< T>

So, how do I implement IEnumerable<T> in a C++/CLI class?

推荐答案

您必须提供 the非通用GetEnumerator()方法,并包含非通用名称空间:

You must provide an explicit implementation of the non-generic GetEnumerator() method and include the non-generic namespace:

using namespace System::Collections;

....

virtual IEnumerator^ EnumerableGetEnumerator() = IEnumerable::GetEnumerator
{
    return GetEnumerator<MyClass^>();
}

更新:如评论中所述, GetEnumerator的显式版本必须命名为不同名称以避免名称冲突,因此我将其命名为EnumerableGetEnumerator。

Update: As mentioned in the comments, the explicit version of GetEnumerator must be named different to avoid name clash, thus I've named it EnumerableGetEnumerator.

类似地,在C#中,您必须这样做: / p>

Similarly, in C# you would have to do it like this:

using System.Collections.Generic;

public class MyCollection : IEnumerable<MyClass>
{
    public MyCollection()
    {
    }  

    public IEnumerator<MyClass> GetEnumerator()
    {
        return null;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator<MyClass>();
    }
}

这篇关于实现IEnumerable&lt; T&gt;在C ++ / CLI中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆