如何在c ++ / cli中调用基类索引器属性?

问题描述 投票:1回答:1

我正在尝试实现一个从通用List<T>类继承的类:

Class MyList::List<MyClass>

我想用一种方法调用List<T>::Item[Int32]索引器属性,但是我不知道该怎么做。

我尝试过List<MyClass>::[i]List<MyClass>::Item[i],但均无效。

我知道,要从List<MyClass>调用其他方法,例如Add,我可以执行List<MyClass>::Add(myInstance)

c++-cli
1个回答
0
投票

由于List<T>.Item[Int32]是默认的索引器,因此您只需执行List<T>.Item[Int32]。例如:

this[i]

或者,如果您愿意,可以显式使用“默认索引器”属性名称public ref class MyList : System::Collections::Generic::List<MyClass ^> { public: int CountNonNull(); }; int MyList::CountNonNull() { int nonNull = 0; for (int i = 0; i < Count; i++) { if (this[i] != nullptr) nonNull++; } return nonNull; }

default[i]

if (this->default[i] != nullptr) nonNull++; 也有效。

请参阅:List<MyClass ^>::default[i]

(顺便说一句,您可能想看问题[[How to: Use Properties in C++/CLI: Indexed properties。它被标记为Why not inherit from List<T>?,但在这里也可能与此有关。)

© www.soinside.com 2019 - 2024. All rights reserved.