Skip to main content
Formatting
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

commentA further suggestion to "An alternative approach": Since

Since the destructor also adds a new line to the screen, if you call two progress bars in the same function they will appear in the reverse order on your screen

for example;For example:

Will returnThat will display: Progress bar 2 ............................................ [100%] Progress bar 1 ............................................ [100%]

Progress bar 2 ............................................ [100%]
Progress bar 1 ............................................ [100%]

because of the order the stack islocal objects are deleted.

Will returnThis will display: Progress bar 1 ............................................ [100%] Progress bar 2 ............................................ [100%]

Progress bar 1 ............................................ [100%]
Progress bar 2 ............................................ [100%]

comment to "An alternative approach": Since the destructor also adds a new line to the screen, if you call two progress bars in the same function they will appear in the reverse order on your screen

for example;

Will return: Progress bar 2 ............................................ [100%] Progress bar 1 ............................................ [100%]

because of the order the stack is deleted.

Will return: Progress bar 1 ............................................ [100%] Progress bar 2 ............................................ [100%]

A further suggestion to "An alternative approach":

Since the destructor also adds a new line to the screen, if you call two progress bars in the same function they will appear in the reverse order on your screen

For example:

That will display:

Progress bar 2 ............................................ [100%]
Progress bar 1 ............................................ [100%]

because of the order the local objects are deleted.

This will display:

Progress bar 1 ............................................ [100%]
Progress bar 2 ............................................ [100%]
Source Link

comment to "An alternative approach": Since the destructor also adds a new line to the screen, if you call two progress bars in the same function they will appear in the reverse order on your screen

for example;

int main()
{
    progress_bar progress1{std::clog, 70u, "Progress bar 1"};

    for (auto i = 0.0;  i <= 100;  i += 3.65) {
        progress1.write(i/100.0);
        // simulate some work
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    progress_bar progress2{std::clog, 70u, "Progress bar 2"};

    for (auto i = 0.0;  i <= 100;  i += 3.65) {
        progress2.write(i/100.0);
        // simulate some work
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

Will return: Progress bar 2 ............................................ [100%] Progress bar 1 ............................................ [100%]

because of the order the stack is deleted.

Instead I suggest you create the objects with dynamic allocations and then delete them:

int main()
{

    progress_bar* progress1 = new progress_bar(std::clog, 70u, "Progress bar 1"); 

    for (auto i = 0.0;  i <= 100;  i += 3.65) {
        progress1->write(i/100.0);
        // simulate some work
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    delete progress1;

    progress_bar* progress2 = new progress_bar(std::clog, 70u, "Progress bar 2"); 

    for (auto i = 0.0;  i <= 100;  i += 3.65) {
        progress2.write(i/100.0);
        // simulate some work
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    delete progress2;
}

Will return: Progress bar 1 ............................................ [100%] Progress bar 2 ............................................ [100%]