0

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.

  • What you "read somewhere here" did you understand what the explanation was saying? It is very important to actually understand the explanation of how C++ works. Blindly copy-pasting code that's found by a keyword search, without fully understanding it, always ends in tears. – Sam Varshavchik Dec 21 '22 at 13:58
  • 1
    (1) pointers to objects so no slicing, (2) object ownership is a mess and `GameInfo` breaks the rule of 3/5/0. (3) `Player*` points to a `Player` which might be a `PlayerBasketball` (or any other derived object) but you don't know this. Suggest you start with [C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Richard Critten Dec 21 '22 at 13:59
  • *I tried mainly to use Base class pointers in the vector, but I still cannot access derived class specific methods* -- You didn't show the code where you are doing this. Did you use `dynamic_cast` to determine if indeed, the `Player*` points to a `PlayerBasketball`? – PaulMcKenzie Dec 21 '22 at 14:05
  • Adding (the address of) a `PlayerBasketBall` to your `vector` does not slice the object. However, given a `Player *`, only operations (member functions) and data (members) of a `Player` (not `PlayerBasketBall`) can be accessed. If you need to call member functions of `PlayerBasketBall`, but those member functions are not part of `Player`, then it is necessary convert the `Player *` to a `PlayerBasketBall *` and use that converted pointer to call `PlayerBasketBall`s member functions. Warning: if the *actual* object is not really a `PlayerBasketBall`, this gives *undefined behaviour* – Peter Dec 21 '22 at 14:07

0 Answers0