我正在尝试实现一个从通用List<T>
类继承的类:
Class MyList::List<MyClass>
我想用一种方法调用List<T>::Item[Int32]
索引器属性,但是我不知道该怎么做。
我尝试过List<MyClass>::[i]
和List<MyClass>::Item[i]
,但均无效。
我知道,要从List<MyClass>
调用其他方法,例如Add
,我可以执行List<MyClass>::Add(myInstance)
。
由于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>?,但在这里也可能与此有关。)