Sign in to follow this  

Virtual function call to child class (Please Help)

This topic is 2122 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..

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
[CODE]std::vector<unique_ptr<Particle-base>> v_particles;[/CODE]

I want to update all the particles in one loop, like this:
[CODE]
void Player::update_particles()
{
for (v_particles_it = v_particles.begin(); v_particles_it != v_particles.end(); v_particles_it++){
(*v_particles_it)->Update();
}
}
[/CODE]

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

This topic is 2122 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this