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 6747457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:26:40+00:00 2026-05-26T12:26:40+00:00

I have a class which contains a static member, a map of strings to

  • 0

I have a class which contains a static member, a map of strings to function pointers. This map is intended to be populated once with a static set of mappings, and will not subsequently be modified.

My question is, how can I ensure the map is not accessed before it is initialised? My code currently looks something like this:

class MyClass
{
  static MapType s_myMap;
public:
  static const MapType& getTheMap()
  {
    if (s_myMap.empty())
    {
      // Populate the map
    }
    return s_myMap;
  }
};

which works fine for external clients of MyClass, but doesn’t prevent internal class members from directly accessing the private map before it has been initialised.

To address that problem I am thinking of making the map local to the getter method:

class MyClass
{
public:
  static const MapType& getTheMap()
  {
    static MapType s_myMap;
    if (s_myMap.empty())
    {
      // Populate the map
    }
    return s_myMap;
  }
};

Is that a good idea, or is there a better way of achieving this?

  • 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-26T12:26:41+00:00Added an answer on May 26, 2026 at 12:26 pm

    Moving the static into the function will solve any order of
    initialization problems, but it may leave you with an order of
    destruction one. In many cases, it’s preferable to use a pointer and
    dynamic allocation, so that the map is never destructed.

    As for initializing it, I often use the two iterator constructor, so
    that I can make the map itself const. To do this, just define a
    struct with a conversion operator, something like:

    struct MapInitData
    {
        char const* key;      //  Or whatever type is needed.
        char const* value;    //  Or whatever type is needed.
        operator MapType::value_type() const
        {
            return MapType::value_type( key, value );
        }
    };
    
    MapInitData const mapInitTable[] =
    {
        { "key1", "value1" },
        //  ...
    };
    
    MapType const ourMap( begin( mapInitTable ), end( mapInitTable ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a C++ class which contains a static member variable, does the
I have a DLL which contains a class with static members . I use
I have a class which contains a static collection that can be used across
I have a parent class which contains a child object. I am using set
I have a header file which contains a member variable declaration of a static
I have a class which contains a static field that acts like a singleton
I have a class named Particle which has a std::set as a member. The
I have a C++ class which contains only static data members. I noticed the
I have a static class which contains a RoutedUICommand that I would like to
I have inherited code which contains static nested classes as: public class Foo {

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.