Post by thetruecoolness on Jan 23, 2007 21:46:45 GMT -5
Typically you only want to control the current object within it's code. If you want to control another object of the same type (like to add to the current one), then you pass it in as a paramter to the function using either a reference of a pointer.
This is the point of Object oriented programming, encapsulation. Also the idea is to section out control, so that you have each individual instance control itself (this is the point of classes they all function the same). So the idea is that you have some other class that contains a list (or some other container) of instances of this class and will iterate through them calling the appropriate method and each instance will update itself accordingly instead of having a global function that operates on one.
Now of course really you're supposed to get it so that classes just call each other, and there is no 'God' class that controls one thing. That way if one instance messes up or gets corrupted your program can still run, and possibly even recover the lost one.
So the point is inside a class you're only supposed to be changing that instance of the class, or a static class variable. That way each instance is responsible for it's own state. So this is only there to access the current instance (which is what you should be doing, as if not you would just make a global function).
For what you're talking about you usually would us a composite class (one that contains a collection of instances from another class), and then write methods in that class to work on the instances of the collection.
So like say your making a particle system. You would have
ParticleSystem be a class with methods
constructor -- Initializes array of Particles
update -- goes through the array and calls the particles update methods
check_collisions -- goes through array and checks if each pair of them collide or not (private called from update)
Particle is another class representing an individual particle
constructor - inits Vel, Pos, and Acc
update - updates Vel and Pos according to Acc
check_collision(Particle *p) - returns a boolean to see if the particle collided with another one
So in particle systems update it would look something like this
void update(void) {
__for (int i= 0: i < this->particles.length; i++) {
____this->particles->update();
__}
}
then in Particle
void update(void) {
__this->acc += this->acc * (time() - this->last_update);
__etc.
}
for the check collision
void Particle::check_collision(Particle *p) {
__do collision checking with p, and this.
}
And if you wanted to control multiple ParticleSystems, then you would either create a class called ParticleSystemController, or just use a global array of them.
NOTE my code might not be correct as it's been a little while since I messed with C++, since I've been using web coding languages for a while (XHTML, CSS, XML, PHP, Javascript).
This is the point of Object oriented programming, encapsulation. Also the idea is to section out control, so that you have each individual instance control itself (this is the point of classes they all function the same). So the idea is that you have some other class that contains a list (or some other container) of instances of this class and will iterate through them calling the appropriate method and each instance will update itself accordingly instead of having a global function that operates on one.
Now of course really you're supposed to get it so that classes just call each other, and there is no 'God' class that controls one thing. That way if one instance messes up or gets corrupted your program can still run, and possibly even recover the lost one.
So the point is inside a class you're only supposed to be changing that instance of the class, or a static class variable. That way each instance is responsible for it's own state. So this is only there to access the current instance (which is what you should be doing, as if not you would just make a global function).
For what you're talking about you usually would us a composite class (one that contains a collection of instances from another class), and then write methods in that class to work on the instances of the collection.
So like say your making a particle system. You would have
ParticleSystem be a class with methods
constructor -- Initializes array of Particles
update -- goes through the array and calls the particles update methods
check_collisions -- goes through array and checks if each pair of them collide or not (private called from update)
Particle is another class representing an individual particle
constructor - inits Vel, Pos, and Acc
update - updates Vel and Pos according to Acc
check_collision(Particle *p) - returns a boolean to see if the particle collided with another one
So in particle systems update it would look something like this
void update(void) {
__for (int i= 0: i < this->particles.length; i++) {
____this->particles->update();
__}
}
then in Particle
void update(void) {
__this->acc += this->acc * (time() - this->last_update);
__etc.
}
for the check collision
void Particle::check_collision(Particle *p) {
__do collision checking with p, and this.
}
And if you wanted to control multiple ParticleSystems, then you would either create a class called ParticleSystemController, or just use a global array of them.
NOTE my code might not be correct as it's been a little while since I messed with C++, since I've been using web coding languages for a while (XHTML, CSS, XML, PHP, Javascript).