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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:11:04+00:00 2026-06-04T05:11:04+00:00

I’m trying to build a progressbar class that can have an arbitrary number of

  • 0

I’m trying to build a progressbar class that can have an arbitrary number of subprogressbars by using something that looks like the composition pattern.

let’s say I have this class pbar:

class pbar
{
    public:
        pbar(const int w) { width = w; } // already sets the
        ~pbar() {}

         void setwidth(const int w) { width = w; } // set the width to w
         void show() const;
         void sync();

         void add(const pbar bar)
         {
              // add's a subbar
              subbars.pushback(bar);
         }

     private:
         std::vector<pbar> subbars; // the sub-process progressbars
         int width;                 // onscreen width of the pbar
};

As you can see the pbar has two members: the width and the subprogressbars (which are themselves pbars). I’ve been trying to implememt a sync function wich changes all the widths of the pbars in subbars to match that of the pbar it was called from:

void pbar::sync()
{
    for ( pbar bar : subbars )
    {
         bar.setwidth(width);  // first set the width of the subbar
         bar.sync();           // secondly make it sync up it's subbars
    }
}

but this does not seem to work. I’ve tried using this test program:

int main()
{
    pbar a(1);
    pbar b(2);
    pbar c(3);
    pbar d(4);

    c.add(d);
    b.add(c);
    a.add(b);

    a.show();
    std::cout << "syncing" << std::endl;
    a.sync();
    a.show();
}

with the show function defined as:

void pbar::show() const
{
    std::cout << w << std::endl;
    for ( pbar bar : subbars )
    {
         bar.show();
    }
}

The expected output would be:

1
1
1
1

yet it is:

1
2
3
4

The strange this is that the show() function does properly iterate down to all the subbars, but it looks like the sync() doesn’t (in fact, using cout I’ve affirmed that in actually does, but it seems to have no effect).

What is wrong with my code? It is not the use of the c++0x type for loop, because I’ve tried using older iterator loops. I cannot find the mistake I made. I think it has something to do with the fact that I’m changing the wrong pbars when using setwidth in sync.

disclaimer: this is actually part of a larger project and the class is a lot more complicated than is shown here, but I’ve managed to reproduce the unwanted behaviour using the above code (which by the way is not copy-pasted and might contain typo’s)

  • 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-04T05:11:06+00:00Added an answer on June 4, 2026 at 5:11 am

    It is not the use of the c++0x type for loop

    Actually, depending on what you really want to do, it might just be the range-based for loop you’re using.

    As posted in the question, the subbars vector stores copies of the objects added to it – that may or may not be what you want. Lets assume that it is what you want. The range-based for loop that you have right now in pbar::sync():

    for ( pbar bar : subbars )
    {
        // ...
    }
    

    iterates over the subbars vector, but the bar variable in this case is itself a copy of each element in that subbars vector. So any changes you make to that variable are simply lost after each iteration of the for loop.

    However, if you change the range-based for loop like so:

    for ( pbar& bar : subbars ) // note the `&`
    {
        // ...
    }
    

    Now, bar is a reference to the object in the subbars vector and changes made to it will ‘stick’.

    Keep in mind, however, that since subbars contains copies of the object that were added to it, those changes will not propagate to the original objects added. Whether or not that’s what you want depends on what it is you want. If you want the changes to propagate all the way to the originals, then you need to store pointers (or smart pointers) to the originals instead of copies, as is mentioned in visier’s answer.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.