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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:01:32+00:00 2026-06-03T00:01:32+00:00

I have multiple processing units that might exist in an array, each with their

  • 0

I have multiple processing units that might exist in an array, each with their own parameters. I would like to communicate the parameters of each processing unit using the context pattern after it was suggested as a solution to another question. However, I am unable to find simple C++ examples of the pattern online. I have made a simplified implementation below for your inspection. The code works and compiles just fine, but am I implementing the pattern correctly? Any and all suggestions for style improvements would be most welcome.

#include <iostream>
#include <sstream>

#include <map>

class cParamsContext
{      
     typedef std::map<std::string, float> myMap_t; //Make the return type of getter less wordy
     myMap_t paramMap;

public:              
    cParamsContext()
    {
        paramMap["a0"] = 1.f;
        paramMap["a1"] = 2.f;
    }

    myMap_t const& getMap() const {return paramMap;} //Return read-only alias   
};

class cProcessUnit
{
    float parameter;
    int id;

public:
    cProcessUnit(cParamsContext &contextObj, int id_) : parameter (0.f), id(id_)
    {
        std::stringstream idStream;
        idStream << id;

        std::string key = std::string( "a" + idStream.str() );

        if(contextObj.getMap().find( key ) != contextObj.getMap().end())        
            parameter = contextObj.getMap().find( key )->second; // https://stackoverflow.com/questions/10402354/using-overloaded-operator-via-an-accessor-function#10402452                 
    }

    float getParam() {return parameter;}
};


int main(int argc, char *argv[])
{
    cParamsContext contextObj;


    for (int nn=0; nn<3; nn++)
    {
        cProcessUnit temp(contextObj, nn);
        std::cout << "Processing unit "  << nn << " param = " << temp.getParam() << std::endl;
    }
}

Furthermore, can you suggest how I could make the parameters within each class update themselves should the parameter map change?

The output looks like this incase you were interested . . . .

Processing unit 0 param = 1
Processing unit 1 param = 2
Processing unit 2 param = 0
  • 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-06-03T00:01:34+00:00Added an answer on June 3, 2026 at 12:01 am

    This does look like a valid implementation. Does it pass your tests? I’m not experienced using a context pattern in this particular way, but it does look fine to me.

    As for updating values, I’m currently doing something very similar in a project I’ve been assigned and I’m using the Observer Pattern. The cParamsContext would be the observable in this case. I’m using a signal/slot / event/delegate implementation of the observer pattern. So far, it has worked wonders for my task.

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

Sidebar

Related Questions

I have multiple column headers that spans of multiple column and I would like
I understand that Javascript doesn't have multiple threads, but I'd like to know if
I have multiple threads that are processing rows from the same table that is
I have a need to create multiple processing threads in a new application. Each
I have multiple URLs that returns zip files. Most of the files, I'm able
I have multiple projects in a couple of different workspaces. However, it seems like
I have multiple classes that all derive from a base class, now some of
I have multiple threads (C# application running on IIS) running that all need to
I have multiple threads processing events. I want to assign a nanosecond timestamp to
I have a problem where I need to synchronize processing for multiple threads across

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.