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: }
Then I define the constructors and functions in the sepparate.cpp files.
Here are the definitions for Object:
Object::Object() { x = y = 0; }; Object::update() { tick ++; }; Object::draw() {
And Unit:
Unit::Unit() : Object() { }; Unit::update() { Object::update();
Everything works fine individually, but I encounter a problem when trying to call functions from within the vector.
vector<Object> objects;
Then I do this in my void main ():
for (int i = 0; i < objects.size(); i ++) { objects[i].update(); objects[i].draw(); };
This draws fine, but only calls the verson update () object, not the version defined by the derived class. Should I create a vector for each type that I get from the Object class for it to work, or is there another way to call derived functions?
Thanks in advance - Seymore
source share