Hey all
I have a dynamic array of pointers to instances of a class ("Player").
The constructor for the Player class looks like this:
Player::Player(void)
{
this->leftChild = 0;
this->rightChild = 0;
srand (time(NULL));
this->id = rand() % 1000;
}
The dynamic array of pointers looks like this:
Tree* PlayerTree = new Tree; //declaring a tree here for use later
Player** players;
players = new Player* [5];
Player* p1 = new Player(1);
players[0] = p1;
Player* p2 = new Player(2);
players[1] = p2;
Player* p3 = new Player(3);
players[2] = p3;
Player* p4 = new Player(4);
players[3] = p4;
Player* p5 = new Player(5);
players[4] = p5;
Then I insert the instances of Player from the array into the tree I declared above using a member function of my Tree class:
PlayerTree->Insert(players[1]);
PlayerTree->Insert(players[0]);
PlayerTree->Insert(players[3]);
PlayerTree->Insert(players[2]);
PlayerTree->Insert(players[4]);
I then call a function of Tree which displays the "id" data member of each node in order (the "id" should be random based on the constructor).
However, I get this:
Player ID: 500 (random number)
Player ID: 500
Player ID: 500
Player ID: 500
Player ID: 500 (all the SAME random number!)
Why might this be? Surely the constructor is called each time a new instance is created, and in the constructor I re-seed the rand() function so it should re-seed each time it is called.
I should point out that the Player class is a child of a class called GameObject whose constructor is blank (default). Could this be a factor?
Thanks for any help!
James