making alot of the same thing

i need to make more than one thing of the same thing. that thing is a ball. so when the user presses space bar it makes a new ball on the screen. i think i can do this with a set of classes maybe. thanks for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
class Ball
{
 // ...
};

// somewhere
std::vector< Ball >  myballs;  // array of balls

// called when space is pressed
void AddBall()
{
  myballs.push_back( Ball() );
}


Note it's probably not most efficient to store the Ball in the vector as per the above example. A better alternative might be to use a std::deque instead so there's less chance of a bulk copy. Or dynamically allocate the balls with new (although that makes cleanup more difficult).
whats the difference between deque and a vector? thanks for the help.
Relevent topics:
http://cplusplus.com/reference/stl/deque/

http://cplusplus.com/reference/stl/vector/


Quote of interest:
Both vectors and deques provide thus a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors are very similar to a plain array that grows by reallocating all of its elements in a unique block when its capacity is exhausted, the elements of a deques can be divided in several chunks of storage, with the class keeping all this information and providing a uniform access to the elements. Therefore, deques are a little more complex internally, but this generally allows them to grow more efficiently than the vectors with their capacity managed automatically, specially in large sequences, because massive reallocations are avoided.
thanks again. one last thing how would i change the first balls info without changing the rest?

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Ball
{
    float positionx;
    float positiony;
    float positionz;
};

vector<Ball>ball;

add()
{
   Ball.positionx = random;
   Ball.Push_back(Ball());
}


sorry if im being a bit annoying. thanks for the help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<Ball> balls;

void func()
{
  // add 3 balls
  balls.push_back( Ball() );
  balls.push_back( Ball() );
  balls.push_back( Ball() );

  // change the 2nd one so it's at 1,2,3
  balls[1].positionx = 1;
  balls[1].positiony = 2;
  balls[1].positionz = 3;
}
thank you! :)

edit :

when i run that balls[1].positionx = 1; it randomly closes. i am sure its this part because i commented it out and the program exicuted fine. this is the exact code.

1
2
3
4
balls[numballs].position[0]0;
balls[numballs].position[1]0;
balls[numballs].position[2]0;
balls[numballs].radius = 0.5f;


can any one please tell me how i can fix


Last edited on
I'm guessing you're stepping out of bounds in your array. Don't do balls[1] unless you have at least 2 balls in the vector. If you have less than 2, you're overflowing the buffer (bad!)

also I don't see how this would compile:
1
2
3
balls[numballs].position[0]0;
balls[numballs].position[1]0;
balls[numballs].position[2]0;


?
ha sorry i increased the number of balls before i changed everything. lol
Topic archived. No new replies allowed.