I'm creating a particle effect system in C++ with SDL. I'm coding a hiarcharchy like this one (scroll a bit down and you will see it).

I have an abstract base class called Particle. It's abstract because I want the derived classes to be forced to implement the pure virtuals (like the Update member function).
Two classes derive from this: ExplosionParticle and TailParticle.

The explosion effect needs to known where to explode (x, y) and does not need to reset its particles when they die out.

The tail effect needs to know which object it is attached to and needs to reset position, velocity etc. in relation to this attached object when it dies out.

The problem is how to call a Reset function from the ParticleEffect class for my TailParticles.. Something like this:

Particles[i]->Reset(attachedTo);

1. If I declare Reset a pure virtual ExplosionParticle, which does not need it, is forced to override it.

2. Declaring Reset as a unique function wont work either since the system relies on polymorphism.

3. Finally, I could declare the it as a normal virtual member function and just not override it in the ExplosionParticle class.

The 3rd option seems to be the best so far, yet I would like a more elegant solution if possible.

I'm also willing to redo my design.

As a final note I'm wondering whether I should let my ParticleEffect class update all the particles by setting their vaules directly (so basically, the Particle class is public and has no functions) or I should have it call each particle's Update member function...

>>s a final note I'm wondering whether I should let my ParticleEffect class update all the particles by setting their vaules directly (so basically, the Particle class is public and has no functions) or I should have it call each particle's Update member function.

Go with update() function.

And could you use the update function instead of the reset function?

Yeah, I decided to go with the Update() function.

I wanted to retain the ideology that the ParticleEffect class takes care of its own particles. I went ahead and made the two Reset functions part of the Particle abstract base class, since a basic particle can be reset in both these ways independant of which kind of particle it is.

I like your suggestion and I'll look into it when the rest of the system is at least working.

Thanks for your help, firstPerson :)

This article has been dead for over six months. Start a new discussion instead.