C++ Dynamic array of pointers?

For various reasons I need to have my next semester of C++ class learn myself so I ask friends who had taken the class for their projects to help me and I'm stuck at this point

So its a game and I have set up 3 classes for the players. The parent Player class, and human and computer class that inherit from the Player class.

Now I'm at the game class and the required attruibute is Player **players, Deck theDeck, int totalPlayers, int dealer. So I assume that the project want me to make an pointer pointer to point to an array or pointers. Then each pointer in the array or pointer will point to each indiviual player data. I get how to do dynamic array but not sure how you do it with pointer.

Is it this?
1
2
  Player ** players;
  *players = new Player[totalplayer]; //Does this make array of pointers? 


And then after I have the array of pointers how do I make the indiviual pointers point to human or computer class?
Does that make it just an dynamic array or an dynamic array or pointers? or is dynamic array always an array of pointers? Thanks

Also how would you code it in two line instead of the one line?
Last edited on
or is dynamic array always an array of pointers?


its probably unlikely you'll see this kind of code. but it shows its possible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
	int num =3;
	int **k= new int*[num];

	int ar1[]= {4,7,8,7};
	int ar2[]= {47,42,24,68,91};
	int ar3[] = {235,78,1,145};


	k[0] = ar1;
	k[1] = ar2;

	cout << k[0][2] << endl; //8

    return 0;
}

I think you misunderstood me. I meant the code

 
int **k= new int*[num]


Have to be done like that? Because I have declared
1
2
3
4
5
Player ** players; 
//in the private section of the class, then I want to make a dynamic array do I have to do 
Player ** players = new Player*[num] 
// Or do  I just do something like this?
players = new Player[num]


Also once I have an dynamic array of my Player class how do I make like each part of the array point at different class? Like My Player Class is the parent class, say the user enter there are 5 players total. Going the easier route of default 1 human player and 4 NPC. My dynamic array be 5 and players[0] would point to a human Class that inherit from player class and players[1]-players[4] would point to computer Class that inherit from Player class. How do I set that up?

Thanks
Last edited on
Topic archived. No new replies allowed.