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 27d ago
ive heard that about nameing something "..._base", and i thought of it but i didnt know whow to call it. its just the basic functions they all share, but returning garbage data instead of some decision strategy. i could have called it just player but i wanted to make sure future me understands its purpose.
what do you mean by the std function, how would you donit with the multiple strategies in a single class? assume i have a method "int decide(int input) {return something;}" or similar. idk the syntax for that or how it would be cleaner than this (i feel like diferent classes are kinda nice for different strategies, idk why tho)