mardi 25 juin 2019

How to create a vector for base class generic types to use concrete type elements

If sb. can change the title to something more understandable I'd be very thankful

This is my current implementation:

std::vector<Zone<sf::CircleShape>*> allZones;                                          
std::vector<Zone<sf::CircleShape>*> estates = initializer->initVillageEstates();       
std::vector<Zone<sf::CircleShape>*> communityAreas = initializer->initCommunityAreas();

I'd love to have something like this:

std::vector<Zone<sf::Shape>*> allZones;                                          
std::vector<Zone<sf::CircleShape>*> estates = initializer->initVillageEstates();       
std::vector<Zone<sf::RectangleShape>*> communityAreas = initializer->initCommunityAreas();

Where CircleShape and RectangleShape derive from the base class Shape. I think that would be possible for a vector if this would be like the generic type for the vector and not the generic type of the generic type of the vector.

One solution that comes in mind is that I make Zone not a template class but like ZoneShape and ZoneCircle : public ZoneShape, ZoneRectangle : public ZoneShape.

That way I could to something like this:

std::vector<ZoneShape*> allZones;                                          
std::vector<ZoneCircle*> estates = initializer->initVillageEstates();       
std::vector<ZoneRectangle*> communityAreas = initializer->initCommunityAreas();

I think that would work but I kinda found the template way more clean for my purpose. So I have to figure out how this might work with that.

Aucun commentaire:

Enregistrer un commentaire