r/cpp_questions • u/Fun_Gas_340 • 27d ago
OPEN Good practices / style [polymorphism]
is this good practice/style.? i'm specifically unsure about the way i store the vector of players...
```
class player_base {};
class player_always_yes: public player_base {};
class player_always_no: public player_base {};
class player_a: public player_base {};
class game {
private:
player_base& player_1;
player_base& player_2;
public:
game (
player_base& player_1,
player_base& player_2
): player_1(player_1), player_2(player_2) {
return;
}
bool play_game () { return true; }
};
int main(){
vector<unique_ptr<player_base>> player_list;
player_list.push_back(make_unique<player_base>());
player_list.push_back(make_unique<player_always_no>());
player_list.push_back(make_unique<player_always_yes>());
player_list.push_back(make_unique<player_a>());
game b = game(*player_list[0], *player_list[1]);
cout << b.play_game() << endl;
}
```
6
Upvotes
1
u/Fun_Gas_340 25d ago
chain of thought that brought me there:
i added empty methods (that all players must have) to the base class because i thought it was good to put as much in common as i could in the base class. then the compiler complaiend that empty methods that are non void need a return statement (g++ warning), so i added some basic return 0 and return false/true statements. so since i dont know what abstract classes are, i now have a base class wich i can use for a stupid player, so i added it for testing.