C++/CLI:实现 IList 和 IList (默认索引器的显式实现)

发布于 2024-07-16 03:07:16 字数 834 浏览 8 评论 0 原文

我正在尝试实现一个同时实现 IListIList 的 C++/CLI 类。

由于它们的名称重叠,我必须显式实现其中之一,自然的选择应该是 IList。

索引器的隐式实现是:

using namespace System::Collections::Generic;
generic<class InnerT> public ref class MyList : public System::Collections::IList, IList<InnerT> {
  // ...
  property InnerT default[int]{
    virtual InnerT get(int index);
    virtual void set(int index, InnerT item);
  }
}

我现在尝试声明 IList 的默认索引器。

我的猜测是这样的:

  property Object^ System::Collections::IList::default[int]{
    virtual Object^ System::Collections::IList::get(int index);
    virtual void System::Collections::IList::set(int index, Object^ item);
  }

但这只是给了我

错误 C2061:语法错误:标识符“默认”

有任何提示吗?

I am trying to implement a C++/CLI class that implements both IList and IList<T>.

Since they have overlapping names, I have to implement one of them explicitly, and the natural choice should be IList.

The implicit implementation of the indexer is:

using namespace System::Collections::Generic;
generic<class InnerT> public ref class MyList : public System::Collections::IList, IList<InnerT> {
  // ...
  property InnerT default[int]{
    virtual InnerT get(int index);
    virtual void set(int index, InnerT item);
  }
}

I am now trying to declare the default indexer for IList.

My guess would be something like this:

  property Object^ System::Collections::IList::default[int]{
    virtual Object^ System::Collections::IList::get(int index);
    virtual void System::Collections::IList::set(int index, Object^ item);
  }

but that just gives me

error C2061: syntax error : identifier 'default'

Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-07-23 03:07:17

JaredPar 的回答 几乎成功了。 应该更改两件事:

  • 索引器属性需要不同的名称,因为隐式实现已采用“默认”。
  • 重写的规范需要在 set 和 get 方法上完成,而不是在属性本身上完成。

IE:

  property Object^ IListItems[int]{
    virtual Object^ get(int index) = System::Collections::IList::default::get;
    virtual void set(int index, Object^ item)  = System::Collections::IList::default::set;
  }

JaredPar's answer almost worked. Two things should be changed:

  • The indexer-property needs a different name since "default" is already taken by the implicit implementation.
  • The specification of the overriding needs to be done on the set- and get-methods, not on the property itself.

I.e.:

  property Object^ IListItems[int]{
    virtual Object^ get(int index) = System::Collections::IList::default::get;
    virtual void set(int index, Object^ item)  = System::Collections::IList::default::set;
  }
ぇ气 2024-07-23 03:07:17

尚未在 C++/CLI 中完成大量接口,但这似乎已涵盖 C++/CLI 规范。 我相信您正在寻找的功能是明确的压倒性的。 在这里,您必须在定义之后指定实现的成员,如下所示。

property Object^ default[int] = System::Collections::IList::default {... }

Haven't done a lot of interfaces in C++/CLI but this appears to be covered 8.8.10.1 of the C++/CLI spec. I believe the feature you're looking for is explicit overriding. In this you must specify the implemented member after the definition like so.

property Object^ default[int] = System::Collections::IList::default {... }
稳稳的幸福 2024-07-23 03:07:17

我编译了一个实现用 C# 显式编写的 IList 的类,并使用 Reflector 打开它并反汇编为 C++/CLI。

T System::Collections::Generic::IList<T>::get_Item(Int32 __gc* index)
{
   //
}

void __gc* System::Collections::Generic::IList<T>::set_Item(Int32 __gc* index, T value)
{
   //
}

但它无法编译:get_Itemset_Item 不是 IList 的成员;

I compiled a class implementing IList<T> explicitly written in C# and opened it with Reflector and disassembled to C++/CLI.

T System::Collections::Generic::IList<T>::get_Item(Int32 __gc* index)
{
   //
}

void __gc* System::Collections::Generic::IList<T>::set_Item(Int32 __gc* index, T value)
{
   //
}

But it doesn't compile: get_Item, set_Item is not a member of IList<T>;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文