2021年5月6日星期四

How can I store instances of related classes into a vector c++

I am having trouble finding the method to store instances of similar classes into a vector. I thought about using a base class, but I am not sure whether it will work. For example, using sports:

class player //base  {     std::string name;     int age;     player(std::string name, int age) name (name), age (age);  };    class soccerplayer: public player  {     float goal_per_game;     //etc  };    class basketballplayer: public player  {     float defensive_blocks;     float three_pointers_per_game;     //etc  };    class hockeyplayer: public player  {     //etc  };    std::vector<player> favoriteplayers;    favoriteplayers.push_back(player("Lionel Messi", 33));  

I am not sure if there is a method to store the various instances of this class. If not, what workaround is possible?

https://stackoverflow.com/questions/67428820/how-can-i-store-instances-of-related-classes-into-a-vector-c May 07, 2021 at 11:58AM

没有评论:

发表评论