1
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

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())
\$\endgroup\$
2
  • \$\begingroup\$ So, a pointer can point to inherited classes of its class? \$\endgroup\$
    – Adam
    Commented Jan 15, 2012 at 9:22
  • \$\begingroup\$ Sure. en.wikipedia.org/wiki/… \$\endgroup\$
    – OCyril
    Commented Jan 15, 2012 at 10:07
6
\$\begingroup\$

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).

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Adam
    Commented Jan 15, 2012 at 9:36
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 15, 2012 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .