Let’s say I was quite bored one late evening and after catatonically staring at the computer monitor for a few hours, I decided to implement an aggregate C++ class to manage colors for drawing pixels, because I’ve obviously gone mad. For starters, we’ll just tell the (probably singleton) ColorManager object what color we want to use and it’ll return a Color object, whatever that may be.
A simple implementation:
#include "Color.h"
#include <map>
enum COLOR { RED = 0, BLUE, GREEN, YELLOW, ORANGE, WHITE, BLACK,
BRICKS_FROM_A_DISTANCE_ON_AN_UNUSUALLY_SUNNY_AFTERNOON,
// etc
COLOR_COUNT };
class ColorManager
{
public:
ColorManager();
~ColorManager();
Color getColor(COLOR color) const;
private:
typedef std::map<COLOR, Color> ColorMap;
static ColorMap colorMap;
};
So, hopefully, this simple code:
ColorManger colorManager;
Color blue = colorManager.getColor(BLUE);
should make it real easy for us to do whatever nonsense for which you’d need Color objects.
The problem is that you need to initialize your static private ColorMap somewhere so that each COLOR enum corresponds to a proper Color object, and VC++ 2010 doesn’t seem to like anything you try. So the question is, how and where do I initialize this map?
Obviously, this is a contrived example, but beyond that, perhaps defining static variables for a class that functions as a singleton is not worth the trouble. Or, perhaps, I might as well just declare the variable static inside of getColor() since that’s the only function that uses it, and just incur the overhead the first time the function is called (although for this simple example, that’s not much better than just putting a giant switch statement in there).
Whatever the case, I appreciate the feedback.
Or with C++11: