# How do I make a pointer point to the current running level if each level is its own class?

I'm hard-coding my levels and coding each level in its own class...The problem is that I need to have a pointer that points to the class of the current level. I don't know how I would go about doing this. I ideally want to have a vector that stores the levels, and I'm working on a level managing class to load, update, draw, and unload the levels in.

If your level classes have a common Base class you can just create a pointers to it:

class Level {
virtual void func1()
//...
virtual void funcN()
}

class Level1: public Level {
virtual void func1()
//...
virtual void funcN()
}

class Level2: public Level {
virtual void func1()
//...
virtual void funcN()
}

Vector<*Level> levels;
levels.append(new Level1())
//...
levels.append(new LevelN())

• So, a pointer can point to inherited classes of its class? – Adam Jan 15 '12 at 9:22
• – OCyril Jan 15 '12 at 10:07

It would be a bad design to have separate classes for each of the level and will also duplicate your code. Why can't you change the level specific values before starting a new level (have them stored in as some constant or in text file).

• I could do it that way with the way my code's going...But the reason I did it this way in the first place was because there were some problems in my game that I only figured out how to solve through putting each level in its own class. – Adam Jan 15 '12 at 9:36
• Yeah @Adam, saurabh.s is right. If you couldn't find a way to solve it with just one class, try asking a question on how to do that, people may help you :) If you find a way to do it with just one class, it might make your postwork a looooot easier. – Gustavo Maciel Jan 15 '12 at 10:36