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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:16:52+00:00 2026-05-27T14:16:52+00:00

I have an object called LastQueryInfo lastQuery in my class. Every time this object

  • 0

I have an object called LastQueryInfo lastQuery in my class. Every time this object changes, I add it to a vector called history.

Initially, when I did history.push_back(lastQuery) I didn’t know what would happen – is the vector going to make a copy of the object? or is it going to keep a reference to it? So if later I modify lastQuery, are all the objects (assuming they are references) in the history vector going to be modified?

After some testing, I found that history.push_back(lastQuery) is indeed going to make a copy of the object, then add it to the vector. But how can I know that without doing any tests? How can I know when C++ is going to make a copy, and when it’s going to add the actual object?

  • 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-27T14:16:53+00:00Added an answer on May 27, 2026 at 2:16 pm

    std::vector always stores a copy† of whatever you push_back(). So modifying the value you passed in will not affect the value stored in the vector. It isn’t like Java or C# where an Object o; is actually a reference to the object, and the object lives until the garbage collector comes and picks it up when the last reference to it goes away. In C++, Object o; is the actual object, which will go away at the end of its scope.

    So if std::vector only stores references to the objects you push_back(), then it will be utterly useless for things like this:

    std::vector<int> numbers;
    for(/* loop condition */) {
        int number = GetUserInput();
        numbers.push_back(n);
    }
    

    Since number will go away at the end of the loop, numbers would hold references to something that will not exist if std::vector was implemented by storing only references. If std::vector actually stored the values, you could access them even after the loop.

    † C++11 supports move semantics, so if the thing you’re pushing back is actually a temporary that will go away soon, it’ll move the internals of the object into the vector storage instead of copying. You can also explicitly use C++11’s std::move() to “force” the move during push_back(). But vector will copy the value in every other case. It’s an implementation detail to optimize the performance of vectors.

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

Sidebar

Related Questions

How would something like this be possible: I have an object called Player: class
I have an object called Time public class Time{ public int Hour {get;set;} public
I have an object class called BOM. It has a method, getChildItem() which returns
I have an object with a property called name. This object has a sub
Hey. I have an object that has a string property called BackgroundColor. This string
I have a data object -- a custom class called Notification -- that exposes
In my JS, I have an object called box_object. It looks like this: ({
I have an object called 'SelectedDay' I instantiate it like this: Day SelectedDay =
I have an object called ValueBox that I created like this: function ValueBox(params) {
I have a object called category, in my view/store/manage.html.erb, I want to do this

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.