Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Virtual function call to child class (Please Help)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 chondee   Members   -  Reputation: 135

Like
0Likes
Like

Posted 19 February 2012 - 12:16 AM

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

Hi,

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++){
  (*v_particles_it)->Update();
}
}

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, 19 February 2012 - 12:57 AM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS