Problem with virtual classes.

I have classes that look like this (simplified)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Object {
  public:
   int width, height;
   virtual void draw() {}
};

class Button : public Object {
  public:
   void draw() {
     /* ... */
   }
};

class Input : public Object {
  public:
   void draw() {
     /* ... */
   }
};


if I have something like this...

Object objects[]

how can I call draw() on all of them?
Java has given me a break from things like this :]...

Plz tel me if im doin it wrong.
Last edited on
You need to use pointers in order to access the virtual methods like this:

1
2
3
4
std::vector <Object*> objects;
for(unsigned int i = 0; i < objects.size(); ++i) {
    objects[i]->draw();
}
thanks.
Topic archived. No new replies allowed.