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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:42:45+00:00 2026-05-16T20:42:45+00:00

VERSION 1 class Doh { private: static std::map<const std::string, const Doh*> someMap; std::string stringValue_;

  • 0

VERSION 1

class Doh {
private:
    static std::map<const std::string, const Doh*> someMap;
    std::string stringValue_;
public:
    Doh(std::string str) : stringValue_(str) {
        Doh::someMap.insert(
            std::make_pair<const std::string,const Doh*>
                (this->stringValue_,this)
        );
    }
}

The above was ok with MSVC 2010 but with MSVC 2008 it fails – and I guess it is because the object is not constructed yet when it is inserted in the map (I got a memory access violation).

So, I tried a delayed insertion, which worked:

VERSION 2

Doh(std::string str) : stringValue_(str) {
    boost::thread(&Doh::insertIntoTheStaticMap,this);
}
void insertIntoTheStaticMap() {
    boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
    Doh::someMap.insert(
        std::make_pair<const std::string,const Doh*>
            (this->stringValue_,this)
    );
}

But as you might be able to guess, my intention is to have the static Doh::someMap as a common lookup dictionary.

VERSION 1 didn’t need any thread-safety because I would create all Doh instances in the same thread – in initialization blocks – which would be called by dynamic initializers before I enter main().

But with VERSION 2, the naïve sleep() is neither graceful nor reliable (not to mention, I might need to lock the map before insertion).

What would be a nice KISS approach?

  • 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-16T20:42:46+00:00Added an answer on May 16, 2026 at 8:42 pm

    Only potential issue I see is the initialization of the static member, if there are multiple source files. Try guarding it with a function.

    class Doh {
    private:
        static std::map< std::string, Doh * > &get_map() {
            static std::map< std::string, Doh * > someMap;
            return someMap; // initialize upon first use
        }
        std::string stringValue_;
    public:
        Doh(std::string str) : stringValue_(str) {
            get_map().insert(
                std::make_pair
                    (this->stringValue_,this)
            );
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the use of overloaded version of Class.forName() ? public static Class<?> forName(String
public class Service{ String serviceName; //setter and getter } public class Version{ int VersionID;
class Person { public string m_name; private int m_age; // << how do I
I suppose that a program like this... class Test { public static void main(String[]
I started with the following code: class Vereinfache2_edit { public static void main(String[] args)
I have a Version class that contains a version_number string, and can obtain a
a more exact version of the code is: class SomeParam; class IBase { public:
I got the following code for my object (short version) : public class PluginClass
public class Version { public byte Major { get; set; } public byte Minor
I have a decorated function (simplified version): class Memoize: def __init__(self, function): self.function =

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.