0

I am writing a card game in C++. I have a game class which keeps track of the players. I also have an abstract base class player, from which the classes person and computer derives.

I would like to keep the players in an array. The number of players is unknown at compile time. Because some players are persons and other computers, I need a player pointer for each, stored in an array, which is dynamically allocated because the number of players is unknown, right?

As I am relatively new to C++, I could not figure out how the syntax looks for this kind of thing.

6
  • 1
    You could simplify the problem by using a vector of pointers to Player. Commented Jan 27, 2014 at 17:36
  • @juanchopanza Simplify it even more by using a vector of Players rather than pointers (cuz pointers in vectors are kindof the devil). Commented Jan 27, 2014 at 17:37
  • 3
    @MadScienceDreams: This would slice the derived objects. Commented Jan 27, 2014 at 17:37
  • @Johnsyweb oh yeah your right, didn't notice it was abstract, my bad... Commented Jan 27, 2014 at 17:38
  • @MadScienceDreams Right idea, wrong implementation, Use smart pointers if you must store pointers in a std::vector. Commented Jan 27, 2014 at 17:39

3 Answers 3

2

For a dynamic array, the standard library provides std::vector.

Since you need to store pointers to an abstract base type, rather than the objects themselves, you'll need to make sure you manage the object's lifetimes correctly. The easiest way is to store smart pointers (in this case std::unique_ptr, for simple single ownership), so that objects are automatically destroyed when they're removed from the vector.

So your array would look like

// Declaration
std::vector<std::unique_ptr<player>> players;

// Adding players
players.push_back(std::unique_ptr<person>(new person("Human"))); // C++11
players.push_back(std::make_unique<computer>("CPU"));            // C++14 (hopefully)

// Accessing players
for (auto & player : players) {
    player->play();
}
Sign up to request clarification or add additional context in comments.

2 Comments

When accessing, what does 'auto' and '&' mean? Also, when I try this in Xcode, it says I need to use '->' instead of '.'
auto means "figure out the type for me"; you could write player instead if you wanted. & means "reference". Xcode is right: you do need -> rather than . to dereference the smart pointer (I've fixed my answer).
1

you need

std::vector<std::shared_ptr<Player>> players;

if you want to use the standard library (which you should)

otherwise

Player** players;

4 Comments

Why shared? I see nothing in OP's post to justify that.
Player** players is not an array.
:) @Johnsyweb . strictly speaking he asked for a pointer to an array, not an array. So you are saying 'Player* is not an array'. You and I know whats behind that statement - he doesn't, so lets not confuse him given that Player * and Player[] will both work fine
But Player * and Player[] are not the same thing! Since the OP is "relatively new to C++", it would be better to explain the differences.
0

You can declare a pointer variable using as the following

Player *playerptr;

It means playerptr is a pointer to type Player.It can hold an address of a Player as well as an array of Players.In C++ and C array is implemented as pointer to the first element of the array.

And as per your requirement you can allocate the array dynamically.

playerptr=new Player[20];

Here 20 is the size of array.

playerptr=new Player[20]();

The second syntax will initialize all the elements in the array to their default value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.