simple polymorphism question

Hi all, I have a simple C++ OOP question regarding a problem I've run into.

I have a Particle class, and ParticleManager class which handles a list of Particles. Then I want to extend these two classes for different types of particles (e.g. Ball and BallManager).

My problem is when I call BallManager balls.update(); it doesn't seem to run the Ball::update function but only the Particle::update();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Particle { 
   friend class ParticleManager; 
    
protected: 
   ...some vars 

   virtual void update(); 

public:    
   Particle(...); 
   ~Particle(); 

}; 


class ParticleManager { 

protected: 
   ...some vars 
    
public: 
   list<Particle*> particles; 

   ~ParticleManager(); 
   virtual void init(...); 
   virtual void add(...); 
   virtual void update(); 
   virtual void render(); 
}; 



/********************* Particle ***********************/ 

Particle::Particle(...) { 
   ...init code 
} 


Particle::~Particle() { 
} 


void Particle::update() { 
} 

  
  
 /********************* Particle Manager ***********************/ 
void ParticleManager::init(...) { 
   ... 
} 

ParticleManager::~ParticleManager() { 
} 


void ParticleManager::add(...) { 
   particles.push_back(new Particle(...); 
   if(particles.size() >= maxCount) { 
      particles.pop_front(); 
      // I probably need to delete the particle as well 
   } 

   printf("PARTICLE add %i \n", particles.size()); 
} 

void ParticleManager::update() { 
   printf("PARTICLE update %i \n", particles.size()); 
   for (list<Particle*>::iterator it = particles.begin(); it != particles.end(); it++) { 
      Particle* particle = *it; 
      particle->update(); 
   } 
} 

void ParticleManager::render() { 
   for (list<Particle*>::iterator it = particles.begin(); it != particles.end(); it++) { 
      Particle* particle = *it; 
      image.draw(particle->x - particle->visible_radius, particle->y - particle->visible_radius, particle->visible_size, particle->visible_size); 
   } 
} 


and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

class Ball : public Particle { 
   friend class BallManager; 
    
protected: 
   ...some more vars and functions 
   void update(); 
    
public: 
   Ball(...); 
    
}; 


class BallManager : public ParticleManager { 
    
public: 
   ~BallManager(); 
    
   void update(); 
   void add(...); 
}; 


Ball::Ball(...) : Particle(...) { 
   ... custom init code 
} 


void Ball::update() { 
   printf("Ball update  \n"); 
   .... custom ball update WHICH IS NOT BEING CALLED 
}      

BallManager::~BallManager() { 
} 


void BallManager::add(...) { 
   ParticleManager::add(...); 
   ... custom add code 
} 



void BallManager::update() { 
   ParticleManager::update(); 
   ... custom manager code which is being called 

} 



maybe its because i'm a bit tired but I can't see what I need to do for the ParticleManager::update() inside BallManager::update() to loop through all the particles but treat them as Balls instead of Particles...

Problem sorted. Solution involves removing the ParticleManager::add() and instead doing:
1
2
3
4
5
6
void ParticleManager::addToList(Particle* particle) {
	particles.push_back(particle);
	if(particles.size() >= maxCount) particles.pop_front();

	printf("ParticleManager::addToList now with %i particles \n", particles.size());
}


and
1
2
3
4
void BallManager::add(... {
	addToList(new Ball(...));
	printf("BallManager::add \n");
}


thanks for looking!

Topic archived. No new replies allowed.