5

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?

2
  • Please change "implements both IList and IList." to "implements both IList and IList<T>". Mar 27, 2009 at 13:11
  • Thanks! It seems markdown was removing the <T>. Mar 27, 2009 at 13:18

3 Answers 3

5

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;
  }
1
2

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 {... }
0

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>;

2
  • 1
    Reflector disassembles into Managed C++ not C++/CLI. Managed C++ is the older managed code extensions. You can tell by all the __gc pointers.
    – Jeff Mc
    Mar 27, 2009 at 13:38
  • There is an extension to Reflector that can generate C++/CLI. Unfortunately, it also gave uncompilable code. Mar 27, 2009 at 13:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.