I'm trying to write my level classes by having a base class that each level class inherits from...The base class uses pure virtual functions.

My base class is only going to be used as a vector that'll have the inherited level classes pushed onto it...This is what my code looks like at the moment, I've tried various things and get the same result (segmentation fault).

class Level
    Mix_Music *music;
    SDL_Surface *background;
    SDL_Surface *background2;

    vector<Enemy> enemy;

    bool loaded;
    int time;
    virtual ~Level();

    int bgX, bgY;
    int bg2X, bg2Y;
    int width, height;

    virtual void load();
    virtual void unload();

    virtual void update();
    virtual void draw();
  bgX = 0;
  bgY = 0;
  bg2X = 0;
  bg2Y = 0;
  width = 2048;
  height = 480;

  loaded = false;
  time = 0; 
//virtual functions are empty...

I'm not sure exactly what I'm supposed to include in the inherited class structure, but this is what I have at the moment...

class Level1: public Level

    void load();
    void unload();

    void update();
    void draw();


  music = NULL;
  background = NULL;
  background2 = NULL;


void Level1::load()
  music = Mix_LoadMUS("music/song1.xm");
  background = loadImage("image/background.png");
  background2 = loadImage("image/background2.png");

  Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 4096);
  Mix_PlayMusic(music, -1); 
void Level1::unload()
//functions have level-specific code in them...

Right now for testing purposes, I just have the main loop call Level1 level1; and use the functions, but when I run the game I get a segmentation fault. This is the first time I've tried writing inherited classes, so I know I'm doing something wrong, but I can't seem to figure out what exactly.

  • 2
    \$\begingroup\$ Here's a tip about a common C++ rookie mistake you're making. Since you intend to use polymorphism, mark ~Level() as virtual too or you will get into problems when trying to call delete on a Level* pointer. \$\endgroup\$ Jan 15, 2012 at 23:48
  • \$\begingroup\$ Did you mean to NULL out your pointers in Level1::~Level1 before calling their free functions? \$\endgroup\$ Jan 16, 2012 at 0:14
  • \$\begingroup\$ I didn't mean to...But I already fixed it and I still have the same problem. \$\endgroup\$
    – Adam
    Jan 16, 2012 at 0:21
  • \$\begingroup\$ What are the "functions have level-specific code in them..."? if the code is just as it is in the question, you doesnt really need to use polymorphism. \$\endgroup\$ Jan 16, 2012 at 0:44
  • 2
    \$\begingroup\$ This seems... really over-designed. Why do you need to have a new class for each level? That will make it really hard and annoying to add more levels in the future. Ideally, you should have a single level class that can handle loading a variety of different levels. \$\endgroup\$
    – thedaian
    Jan 16, 2012 at 0:47

1 Answer 1


You're doing it wrong. Making a class for each level? No, levels are data, not code. Make a generic Level class, and then load .level (or whatever) files into it. The files then should just have a list of all the resources that need to be loaded, a list of events/scripts, and so on.

Why? Because then you can make a separate level editor, that creates the .level files for you, and you don't even have to touch your game's code (which generates bugs and so on).

Classes are here to group similar behaviors and data (objects) together, creating a new class for each instance of an object is illogical and defies the very purpose of classes.

  • \$\begingroup\$ I actually disagree. For a large scale game engine you may be able to put everything into scripts that run based off of triggers but for a small game it may be easier to simply make each level a class that has special coding especially if you want each level to do drastically different things. \$\endgroup\$
    – NtscCobalt
    Sep 29, 2012 at 17:07
  • \$\begingroup\$ First, that's a pretty bad practice. Just because you can do it, doesn't mean you should. What if you want to scale up your game? Add another 20 levels? There are even simpler ways of making the levels drastically different, for example, just including function-pointers in a vector, and then executing them when needed... \$\endgroup\$
    – jcora
    Sep 29, 2012 at 17:13

You must log in to answer this question.

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