A very cool trick I recently found in the gameswithin blog is to put enums into seperated namespaces.

This improves readability of your source and avoids symbol conflicts. Here is a simple example:

#include <iostream>

using namespace std;

namespace ScreenId {
    enum Enum {
        SplashScreen,
        Intro,
        MainMenu,
        Options,
        Credits,
        NumScreens
    };
};

ScreenId::Enum GetCurrentScreen() {
    return ScreenId::MainMenu;
}

int main (int argc, char * const argv[]) {    
    cout << GetCurrentScreen() << endl;
    return 0;
}
  1. James (2011-01-28 07:40)

    A slight variant of this which is more broadly useful is putting enums in a struct followed by a typedef. Using this variant allows you to move enum declarations inside a class. i.e:class ScreenManager{ struct ScreenIds { enum ScreenId { SpashScreen, Intro, MainMenu }; }; typedef ScreenIds::ScreenId ScreenId;ScreenId mCurrentScreen;ScreenManager() { mCurrentScreen = ScreenIds::MainMenu; }ScreenId getCurrentScreenID() { return mCurrentScreen; }};Sorry for the ill formatted code. Here is a cleaner version on codepad:http://codepad.org/nx9gOxEF

Add your comment now