Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:18:00+00:00 2026-05-22T21:18:00+00:00

Let’s say I was quite bored one late evening and after catatonically staring at

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:18:00+00:00Added an answer on May 22, 2026 at 9:18 pm
    #include <map>
    #include "Color.h"
    
    enum COLOR
    {
        RED = 0, BLUE, GREEN, YELLOW, ORANGE, WHITE, BLACK,
        BRICKS_FROM_A_DISTANCE_ON_AN_UNUSUALLY_SUNNY_AFTERNOON,
        // etc
        COLOR_COUNT
    };
    
    class ColorManager
    {
        typedef std::map<COLOR, Color> ColorMap;
    
    public:
        ColorManager();
        Color getColor(COLOR color) const;
    
    private:
        static ColorMap createColorMap();
        static ColorMap colorMap;
    };
    
    // in some .cpp file:
    
    ColorManager::ColorMap ColorManager::createColorMap()
    {
        ColorMap ret;
        // populate ret
        return ret;
    }
    
    ColorManager::ColorMap ColorManager::colorMap = ColorManager::createColorMap();
    

    Or with C++11:

    #include <map>
    #include "Color.h"
    
    enum COLOR
    {
        RED = 0, BLUE, GREEN, YELLOW, ORANGE, WHITE, BLACK,
        BRICKS_FROM_A_DISTANCE_ON_AN_UNUSUALLY_SUNNY_AFTERNOON,
        // etc
        COLOR_COUNT
    };
    
    class ColorManager
    {
        using ColorMap = std::map<COLOR, Color>;
    
    public:
        ColorManager();
        Color getColor(COLOR color) const;
    
    private:
        static ColorMap colorMap;
    };
    
    // in some .cpp file:
    
    ColorManager::ColorMap ColorManager::colorMap = []
    {
        ColorMap ret;
        // populate ret
        return ret;
    }();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I wish to connect to a remote computer through IP address, and
Let's say I have one class User, and it has a property of type
Let's say I have a user enter the URL of an image. After URL
Let's say that I've got a sheet - number one - with over 5000
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let me try to explain by example. Say website is hosted at example.com (NOT
Let's say I have a table with a Color column. Color can have various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.