Implementando GetEnumerator en C ++
-
03-07-2019 - |
Pregunta
En C # tengo la siguiente clase y se compila perfectamente:
clase CustomItem
{
}
class CustomList : IList<CustomItem>
{
public CustomItem this[int index]
{
get { return null; }
set { throw new NotImplementedException(); }
}
public void CopyTo(CustomItem[] array, int arrayIndex)
{
}
public int Count { get { return 10; } }
public int IndexOf(CustomItem item) { throw new NotImplementedException(); }
public void Insert(int index, CustomItem item) { throw new NotImplementedException(); }
public void RemoveAt(int index) { throw new NotImplementedException(); }
public void Add(CustomItem item) { throw new NotImplementedException(); }
public void Clear() { throw new NotImplementedException(); }
public bool Contains(CustomItem item) { throw new NotImplementedException(); }
public bool IsReadOnly { get { return true; } }
public bool Remove(CustomItem item) { throw new NotImplementedException(); }
public IEnumerator<CustomItem> GetEnumerator() { throw new NotImplementedException(); }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ throw new NotImplementedException(); }
}
Cuando intento lo mismo en C ++ obtengo varios errores de compilación:
clase ref CustomItemValue
{
};
typedef CustomItemValue^ CustomItem;
ref class CustomList : public IList<CustomItem>
{
public:
property CustomItem default[int]
{
virtual CustomItem get(int index) { return nullptr; }
virtual void set(int index, CustomItem value) {}
}
virtual void CopyTo(array<CustomItem>^ array, int arrayIndex)
{
}
property int Count { virtual int get() { return 10; } }
virtual int IndexOf(CustomItem item) { throw gcnew NotImplementedException(); }
virtual void Insert(int index, CustomItem item) { throw gcnew NotImplementedException(); }
virtual void RemoveAt(int index) { throw gcnew NotImplementedException(); }
virtual void Add(CustomItem item) { throw gcnew NotImplementedException(); }
virtual void Clear() { throw new NotImplementedException(); }
virtual bool Contains(CustomItem item) { throw gcnew NotImplementedException(); }
property bool IsReadOnly { virtual bool get() { return true; } }
virtual bool Remove(CustomItem item) { throw gcnew NotImplementedException(); }
virtual IEnumerator<CustomItem>^ GetEnumerator() { throw gcnew NotImplementedException(); }
virtual System::Collections::IEnumerator^ GetEnumerator()
{ throw gcnew NotImplementedException(); }
};
Los mensajes de error del compilador son:
. \ mc.cpp (38): error C2556: 'System :: Collections :: IEnumerator ^ CustomList :: GetEnumerator (void)': la función sobrecargada difiere solo por el tipo de retorno de 'System :: Collections :: Generic :: IEnumerator ^ CustomList :: GetEnumerator (void) '
con
El
T = CustomItem
]
. \ mc.cpp (36): vea la declaración de 'CustomList :: GetEnumerator'
. \ mc.cpp (38): error C2371: 'CustomList :: GetEnumerator': redefinición; diferentes tipos básicos
. \ mc.cpp (36): vea la declaración de 'CustomList :: GetEnumerator'
¿Puede alguien ayudarme con esto?
Solución
Debe utilizar la sintaxis de anulación explícita específica de Microsoft para anular ambos métodos GetEnumerator ():
virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
{ throw gcnew NotImplementedException(); }
virtual IEnumerator<CustomItem>^ GetEnumerator()
{ throw gcnew NotImplementedException(); }
Tenga en cuenta que cambié el nombre del método GetEnumerator no genérico a GetEnumerator2 y luego especifica que reemplaza a System :: Collections :: IEnumerable :: GetEnumerator. Puede encontrar más información sobre la anulación explícita en aquí
Otros consejos
Bueno, tienes una pista en el mensaje de error. su método GetEnumerator devuelve System :: Collections :: IEnumerator ^ mientras que hay otro método para esta clase que devuelve System :: Collections :: Generic :: IEnumerator ^, este método probablemente se hereda de la clase IList.
Intente reemplazar el valor de retorno con System :: Collections :: Generic :: IEnumerator ^.