Array and 2D Class Object array

The code below compiles and run fine. My question: is it good/bad programming to use a pointer array class? I only use the pointer object array because I'm not sure how to get it working without a pointer.

So there is code here for an array and a 2d array. Obviously I don't need a 2d array for a player object. I was only testing it. I'll be using it for the tile based map grid later.


array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{

	// Game = Class
	// player = Object
	Game * player[4];
	for (int i = 0; i < 4; i++)
	{
		player[i] = new Game();
		player[i]->setstats(1, 1);
		player[i]->printstats();
	}

	return 0;
}



2d array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	
	Game **player = new Game*[10];
	for(int i = 0; i < 10; i++)
	{
		player[i] = new Game[20];
	}
	player[9][19] = Game();
	player[9][19].setstats(1, 1);
	player[9][19].printstats();

	return 0;
}



And one more question. How do I delete the pointer when I'm done? (for both array and 2d array).
Last edited on
To delete, simply do the opposite of your "new" command.

array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	// Game = Class
	// player = Object
	Game * player[4];
	for (int i = 0; i < 4; i++)
	{
		player[i] = new Game();
		player[i]->setstats(1, 1);
		player[i]->printstats();
	}
	for(int i = 0; i < 4; i++) {
		delete player[i];
	}

	return 0;
}


2d array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{

	Game **player = new Game*[10];
	for(int i = 0; i < 10; i++)
	{
		player[i] = new Game[20];
	}
	player[9][19] = Game();
	player[9][19].setstats(1, 1);
	player[9][19].printstats();

	for(int i = 0; i < 10; i++) {
		delete player[i];
	}
	delete player;

	return 0;
}


As for the good/bad thing... it depends. You may wish to look at collections classes such as vector and list, as they add flexibility you may need. Sorry for the inconclusive answer, but more information is required for me to give a better answer. And even with more information, my answer would probably only be an opinion.
For the 2D array, you used operator new[] so you should use operator delete[]

1
2
3
4
5
6
for(int i = 0; i < 10; i++)
{
    delete [] player[i];
}

delete [] player;
Oops. Thanks for the catch, shacktar.
Thanks Shacter and JMJAtlanta.
Last edited on
Sorry to have to ask this. How would I make a 3d object array? I have searched the net over and over and can only see examples of how to make a regular (non class) 3d array pointer. I will need this to make the map work properly.
honestly, don't use multidimensional arrays at all. They're ugly and annoying.
honestly, don't use multidimensional arrays at all. They're ugly and annoying.

Perhaps you may find this, but for what I'm doing I feel I have no other choice.

I want to make a 2 dimensional map with each x and y coordinate an object, so that would be like map[x][y]. But, there will be several maps and so it turns into something like map[i][x][y], where 'i' is the map number. Thanks to previous help I already have the multi-player system running fine. It's going to be a blast having 3 or 4 players at a time. I just need info on how to make a 3d object array.

I don't like to ask because I know it can be a lot to type, so I try my best to answer at least one question for every question I ask. Anyways, if anyone has any input it would be appreciated.


Perhaps you may find this, but for what I'm doing I feel I have no other choice.


Perhaps you may feel this, but you're wrong. A multidimensional array may be the intuitive solution for such a situation for a newbie, but it's far from the best, or even a good idea.
Perhaps you may feel this, but you're wrong. A multidimensional array may be the intuitive solution for such a situation for a newbie, but it's far from the best, or even a good idea.


I've taken your advice and I'll be putting the map on hold until I find a better way. I'm about half way through the C++ Primer and there's bound to be something in the rest of the pages.

Anyways, thank you hanst99 for bringing me to my senses.
Topic archived. No new replies allowed.