Language
C++
Compiler
gcc 12.1.0
Options
Warnings
Don't Use Boost
C++11
no pedantic
#include <iostream>
#include <cstring>
class Player {
private:
char* name;
int elo;
int score;
public:
Player(const char* const name, const int elo);
~Player();
// The accessor, mutator and print functions are given
int getELO() const { return elo; }
int getScore() const { return score; }
void addScore(const int points) { score += points; }
void print() const {
std::cout << name << " (" << elo << ")";
}
};
Player::Player(const char* const name, const int elo) {
this->name = strdup(name);
this->elo = elo;
this->score = 0;
}
class PlayerList {
private:
int numPlayers;
Player** players;
public:
PlayerList();
PlayerList(const PlayerList& list);
~PlayerList();
// The following accessor functions are given
int getNumPlayers() const { return numPlayers; }
Player* getPlayer(const int index) const { return players[index]; }
void addPlayer(Player* const player);
void sort();
PlayerList* splice(const int startIndex, const int endIndex) const;
};
PlayerList::PlayerList(){
numPlayers = 1;
players = new Player*[1];
players[0] = new Player("a", 0);
}
PlayerList::PlayerList(const PlayerList& list){ std::cout << "don't use me!" << std::endl; }
PlayerList::~PlayerList(){}
int main(void) {
int some_integer_not_important_to_question = 1;
PlayerList* Groups = new PlayerList [some_integer_not_important_to_question];
Groups[0].getPlayer(Groups[0].getNumPlayers() - 1)->print(); //This works
Groups[0].getPlayer(Groups[0].getNumPlayers() - 1)->addScore(2); //This doesn't work
return 0;
}
$ g++ prog.cc -Wall -Wextra -std=c++11
a (0)
Exit Code:
0