jeudi 13 juin 2019

Is it possible to specify abstract class variable type through a template?

In a API context, I'd like to minimize the classes available to the user. To that end, I'd like to generalize the type of data he has access to but specialize inputs data.

I thought of templates. However, I come from the C# world and I'm not used to possibilities and restrictions of templates in the C++ world.

Here is some pseudo-code:

GenericAnimal<T>
bool setSubSpecies(T.EnumOfSubSpecies subSpecies)

GenericAnimal<Fish> cod;
fish.setSubSpecies(Fish.FishSubSpeciesEnum.Cod);
GenericAnimal<Mammal> cetaceans;
cetaceans.setSubSpecies(Mammal.MammalSubSpeciesEnum.Cetaceans);

The alternative would be to create a class Fish that derives from GenericAnimal<T1, T2, ...> with appropriate template classes, but as I said I'd like to avoid the profusion of classes and define T1, T2, ... through a unique class/structure.

Aucun commentaire:

Enregistrer un commentaire