The accepted pattern for doing this is making the base class method protected
:
ref class ItemBase { }
ref class ItemA : ItemBase { }
ref class ItemList abstract {
protected:
virtual void addInternal(ItemBase ^p);
}
ref class ItemListA : ItemList {
public:
virtual void add(ItemA ^p){addInternal(p);}
}
Here is a better solution using generics. Note how we constrain the generic parameter T
to ItemBase
, to enforce that this collection must only ne used with ItemBase
or its subclasses.
ref class ItemBase { };
ref class ItemA : public ItemBase { };
generic <class T>
where T: ItemBase
ref class ItemList abstract {
public:
virtual void Add(T p){}
};
ref class ItemListA : ItemList<ItemA^> {
//no need to override Add here
};
//usage
int main(array<System::String ^> ^args)
{
ItemListA^ list = gcnew ItemListA();
list->Add(gcnew ItemA());
}