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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:30:05+00:00 2026-05-26T10:30:05+00:00

could somebody please explain why this happens? I have written the following: 1) a

  • 0

could somebody please explain why this happens? I have written the following:

1) a WindowManager class which is implemented as a singleton and has an Instance() method, defined like so:

static WindowManager * instance_;
//...
WindowManager * WindowManager::instance_ = 0;
WindowManager & WindowManager::Instance()
{
    if (!instance_)
        instance_ = new WindowManager();
    return *instance_;
}

2) a WindowManager::createWindow method that returns a reference to a newly created window, defined like so:

Window & WindowManager::createWindow()
{
    windows_.push_back(Window());
    return windows_.at(windows_.size() - 1);
}

3) a Window::print method that prints a message inside the window

In my main program, I have written the following:

ui::Window & win1 = ui::WindowManager::Instance().createWindow();
ui::Window & win2 = ui::WindowManager::Instance().createWindow();
win1.print("First window");
win2.print("Second window");

This does not work! Only the second call to print is executed (for win2).
However, if I change the order, like so:

ui::Window & win1 = ui::WindowManager::Instance().createWindow();
win1.print("First window");
ui::Window & win2 = ui::WindowManager::Instance().createWindow();
win2.print("Second window");

then everything works as expected. If anybody could shed some light on this situation, any help would be greatly appreciated.

  • 1 1 Answer
  • 1 View
  • 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-26T10:30:05+00:00Added an answer on May 26, 2026 at 10:30 am

    Here you have quick example which illustrates the problem:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct T
    {
        int id;
        T(int id) : id(id)
           { cout << "created " << id << endl; }
        T(T const& t) : id(t.id)
           { cout << "copy: " << t.id << endl; }
        void print(char const* m)
           { cout << id << ": " << m << endl; }
    };
    
    vector<T> ts;
    
    T& create(int id)
    {
        ts.push_back(T(id));
        return ts.at(ts.size() - 1);
    }
    
    int main()
    {
        // Uncomment these lines and compare results
        //int const max_windows = 10;
        //ts.reserve(max_windows);
    
        T& t1 = create(1);
        T& t2 = create(2);
        t1.print("t1");
        t2.print("t2");
    }
    

    Compile and run as it is and see what is printed to stdout.
    Then uncomment reserve() call, compile and run once again and compare with previous output.

    The problem is that std::vector::push_back causes reallocation of the data internally. This invalidates all references, pointers or iterators to vector elements you’ve obtained before the reallocation.

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

Sidebar

Related Questions

Could somebody please explain what this notation is in javascript? What is function(d) doing?
Could somebody please explain the meaning of this error? INFO: Scanning for root resource
Could somebody please explain to me why does this code only print 42 instead
I am confused uisng joins method in Threads Could somebody please explain I have
Could somebody please explain this to me. It seems to me that the number
Could somebody please explain to me what happens here? I am creating a binding
Could somebody please explain this piece of Ruby code: def add_spec_path_to(args) # :nodoc: args
Could somebody please explain what GCC is doing for this piece of code? What
Could somebody please explain or link to a resource that will tell me why
could somebody please take a look at this http://jsfiddle.net/bloodygeese/EzkFR/1/ My aim is to on

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.