I noticed that I’m often in the need of a container class. For example when working on a particle system, I create a container class Particles which has a member vector<Particle*>. Then I call: Particles* my_particles like my_particles->draw(), and in the Particles.draw() I iterator over the vector<Particle*> and call draw() on each of the particles again. The same works for member functions like update(), addforce() etc.. Now, I’m working on a project and need a collection of Cube on which I need to call tween(), moveTowards() etc..
I know I can use template, but in the case of a template class the member functions need to be knows before. As I want to check if I can make a generic class, that I can use for example both my Cubes and Particles collections.
Someone who has done this before or can give me some advice on this?
Kind regards,
Pollux
The short answer is that you can’t do this in c++. You can, however, use STL algorithms and containers to wrap this behavior up.
First, you’d put your Cube or Particle instances into a
std::vectoror other container (like you have now).Then you’d use STL’s std::for_each in combination with std::mem_fun.
It’d result in something like this: