I have 3 classes:
GameInfo, GameInfoFootball and GameInfoBasketball, the last of 2 of those inherit from GameInfo. GameInfo classes should be able to store Players -> Basketball players and Football Players in the vector team1 and vector team2 described below.
In GameInfo I have the following:
class GameInfo {
public:
vector<Player*> &getTeam1();
vector<Player*> &getTeam2();
protected:
vector<Player*> team1;
vector<Player*> team2; // players in team 2
vector<Player*> team1Sup; // players in team 1
vector<Player*> team2Sup; // players in team 2
I would love to be able to add BasketballPlayers and FootballPlayers to the vector<Player*> without them being sliced to Player. I used a vector of pointers as I read somewhere here, but the object still gets sliced. For example, here's the BasketBallPlayer class:
class PlayerBasketball : public Player{
public:
int getTempoEntrou() const;
int getTempoSaiu() const;
int getTotalPontos() const;
PosicaoBAS getPos() const;
PlayerBasketball(string nome, PosicaoBAS pos);
void point();
Whenever I fill the vector<Player*> with BasketBallPlayers references I am unable to access specific BasketBallPlayers methods/atributes. Can someone explain me what's going on?
This is the code that adds Players to the vector:
PlayerFootball p = PlayerFootball(split(line, ",", true),
convertStringInEnumFUT(split(line, ",", false)));
GameInfo::addPlayerTitu("team1", Player *p)
void GameInfo::addPlayerTitu(string team, Player *p) {
if (team == "team1") {
team1.emplace_back(p);
} else if (team == "team2") {
team2.emplace_back(p);
}
}
I tried mainly to use Base class pointers in the vector, but I still cannot access derived class specific methods, like getTempoEntrou(), only the Player methods.