Jump to content
  • Advertisement
Sign in to follow this  

Virtual function call to child class (Please Help)

This topic is 2798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

<SOLVED> I was doing something wrong, unrelated to what I thought the problem is..


I am writing a simple particle engine for my game, and for performance reasons, instead of having one type of particle class for every type of particle, I now rewrote the code, such that there is a Particle base class, and many Particle child classes (flash, smoke, explosion, flame).

All the different kinds of particle objects are stored in a
std::vector<unique_ptr<Particle-base>> v_particles;

I want to update all the particles in one loop, like this:

void Player::update_particles()
for (v_particles_it = v_particles.begin(); v_particles_it != v_particles.end(); v_particles_it++){

Each different child class of Particle-base have their own Update() function.

I want (*v_particles_it)->Update(); to call the Update() function of each particle's respective Update() function, NOT Particle-base's Update() function.

If I don't include an Update() function for Particle-base, I get a compiler error (even though all particles in the vector are child objects), or if I define it as virtual void Update(), it only calls Particle-base's Update() function.

I don't have too much experience in inheritance, I know I am most likely doing this wrong.
Could someone please help me how I should set up the base and child class' functions so they are called the way I described above?

Thank you in advance! Edited by chondee

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!