I have an array of pointers to a class called cPlayer.
Compiler: Visual C++ 2010 Express
Init
cPlayer* players[MAX_PLAYERS];
Max_Players is a defined value of 10.
defined in a class called “OnlineData”, in a header.
Checking
if (players[a]){
// some code here
}
What ever I have tried, it still gets through to //some code here
Players are deleted like this:
players[player->id]=0;
delete player;
Question
I want to check if item at position a in the players array has a value. I am using it to resort the player list (defragmentation)
The array works normalling in adding content, until i try to do the above thing
To initialize a global or local array you would use:
However since your array is a class member you can’t do this (until C++11 introduced in-class initialization, but VS2010 does not support this feature). Instead you have to write code that initializes each member individually in your constructor bodies (or init function or whatever):
Or better than using an explicit loop would be to use something from
<algorithm>, such asstd::fillorstd::fill_n: