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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:58:11+00:00 2026-05-20T08:58:11+00:00

Trying to learn something new every day I’d be interested if the following is

  • 0

Trying to learn something new every day I’d be interested if the following is good or bad design.

I’m implementing a class A that caches objects of itself in a static private member variable std::map<> cache. The user of A should only have access to pointers to elements in the map, because a full copy of A is expensive and not needed. A new A is only created if it is not yet available in the map, as construction of A needs some heavy lifting. Ok, here’s some code:

class B;

class A {
    public:
        static A* get_instance(const B & b, int x) {
            int hash = A::hash(b,x);
            map<int, A>::iterator found = cache.find(hash);
            if(found == cache.end())
                found = cache.insert(make_pair(hash, A(b,x))).first;
            return &(found->second);
        }
        static int hash(B & b, int x) { 
            // unique hash function for combination of b and x
        }
        // ...

    private:
        A(B & b, int x) : _b(b), _x(x) { 
            // do some heavy computation, store plenty of results 
            // in private members
        }
        static map<int, A> cache;
        B _b;
        int _x; // added, so A::hash() makes sense (instead of B::hash())
        // ...
 };

Is there anything that is wrong with the code above? Are there any pitfalls,
do I miss memory management problems or anything else?

Thank you for your 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-20T08:58:11+00:00Added an answer on May 20, 2026 at 8:58 am

    The implementation is intended to only allow you to create items via get_instance(). You should ideally make your copy-constructor and assignment operator private.

    It would not be thread-safe. You can use the following instead:

    const boost::once_flag BOOST_ONCE_INIT_CONST = BOOST_ONCE_INIT;
    
    struct AControl
    {
      boost::once_flag onceFlag;
      shared_ptr<A> aInst;
    
      void create( const B&b, int x )
      {
          aInst.reset( new A(b, x) );
      }
    
      AControl() : onceFlag( BOOST_ONCE_INIT_CONST )
      {
      }
    
      A& get( const B&b, int x )
      {
         boost::call_once( onceFlag, bind( &AOnceControl::create, this, b, x ) );
         return *aInst;
      }
    };
    

    Change the map to
    map

    Have a mutex and use it thus:

    AControl * ctrl;
    {
      mutex::scoped_lock lock(mtx);
      ctrl = &cache[hash];
    }
    return ctrl->get(b,x);
    

    Ideally only get_instance() will be static in your class. Everything else is private implementation detail and goes into the compilation unit of your class, including AControl.

    Note that you could do this a lot simpler by just locking through the entire process of looking up in the map and creating but then you are locking for longer whilst you do the long construction process. As it is this implements record-level locking once you have inserted the item. A later thread may find the item uninitialised but the boost::once logic will ensure it is created exactly once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new in MVC3 and trying learn something. using Razor view engine. mysql database.
Trying to learn something new - specifically trying to choose wether to use MySQLi
I am trying to learn something about WPF and I am quite amazed by
This might sound like a stupid question but just trying to learn something here.
I'm trying to learn MVVM, but there is something I don't understand yet. Currently,
Trying to learn MVP pattern with C#... Does anyone know of any particularly good
im trying to learn delegates and events in c#, i understand that an event
I'm trying to take full advantage of object oriented php and learn something along
I am trying to do something in Rails and will admit that I'm no
I am trying to learn using MonoTouch. I am basically trying to create something

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.