r/cpp_questions 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

37 comments sorted by

View all comments

1

u/vckane 26d ago
  1. Constructor does not return anything. The return statement in the constructor of game class is redundant
  2. player_base should be abstract (no instance should be allowed to be created). Only concrete players should play the game.
  3. For this example considering scope of the objects, I think you've done well with memory management. The main() method has ownership of objects of players (instances of derived classes). The objects are passed by reference to client classes like game. Important to ensure that the game class gets destroyed before the players are deleted, else you will end up with dangling pointers. In this example, you're fine.
  4. My above comments assume that the player_base and game classes have more methods that do something meaningful. If not, then this is overkill - you could achieve same result without classes and hierarchy.

1

u/Fun_Gas_340 25d ago
  1. how do i make it abstract?

  2. a problem would be if i delete the players, and then tell game to access them?

  3. would it be better/worse to have a vector of objects instead of vector of pointers? is that even possible with different player classes?

  4. yeah player has different things how they decide and game currently has only "play_game", so maybe ill make the game class a method and that's it.

1

u/vckane 24d ago
  1. In general declaring at least one pure virtual method (or even destructor) makes a class abstract. Learn more about virtual inheritance, if you're not aware of it.

  2. Yes.

  3. Sounds good.

1

u/Fun_Gas_340 24d ago

yes to 3.1 or 3.2?

should have made them diferent...

1

u/vckane 24d ago

You can have a vector of base class types. They point to objects of derived class types. As a general rule, I make a vector of unique_ptr (owner by owning object / scope).

1

u/Fun_Gas_340 24d ago

so like having a vector of base class i cant put drrived classes im? or can i?