I have two classes:
class Object {
public:
Object();
virtual void update();
virtual void draw();
private:
protected:
int x, y, tick;
}
and
class Unit : public Object {
public:
Unit();
void update();
private:
protected:
}
I then define the constructors and functions in sepparate .cpp files.
Here's the definitions for Object:
Object::Object() {
x = y = 0;
};
Object::update() {
tick ++;
};
Object::draw() {
// All my draw code is in here.
};
And Unit:
Unit::Unit() : Object() {
};
Unit::update() {
Object::update();
// Then there's a bunch of movement related code here.
};
Everything works fine individually, but I run into a problem when trying to call functions from within a vector.
vector<Object> objects;
I then do this in my void main():
for (int i = 0; i < objects.size(); i ++) {
objects[i].update();
objects[i].draw();
};
This draws everything fine, but it only calls the Object verson of update() not the version as defined by the derived class. Do I have to make a vector for each type that I derive from the Object class for it to work, or is there another way to call the derived functions?
Thanks in advance - Seymore