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

  • Home
  • SEARCH
  • 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 8681685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:34:30+00:00 2026-06-12T21:34:30+00:00

I want to store a list of objects in an std::vector , but the

  • 0

I want to store a list of objects in an std::vector, but the objects contain a reference and cannot be assigned to. I can, however, copy-construct the object.

The only option I can think of is to use pointers to wrap the objects and reseat the pointers when they need to be assigned, but the syntax for this would significantly reduce readability, especially when using iterators, and I would prefer an alternative.

Doesn’t work:

std::vector<MyObject> myVector;
//fill the vector
//...
myVector[1] = object1;

Smart pointers sacrifice readability:

std::vector<std::unique_ptr<MyObject>> ptrVector;
//fill the vector
//...
ptrVector[1] = std::unique_ptr<MyObject>(new MyObject(object1));

Are there any other methods to use unassignable objects in an std::vector?

  • 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-12T21:34:31+00:00Added an answer on June 12, 2026 at 9:34 pm

    This isn’t a direct answer to your question as I can’t offer a replacement for std::vector, or a different way of using it that allows you to do what you need to.

    However, if it is possible to modify the definition of MyObject, it may be an option to change it so it uses std::reference_wrapper instead of conventional references. That would allow MyObject to be assignable.

    Example:

    #include <vector>
    #include <functional>
    #include <iostream>
    
    struct MyObject
    {
      //int &_i;
      std::reference_wrapper<int> _i;
      MyObject(int &i):_i(i) {}
    };
    
    
    int main() {
      std::vector<MyObject> vec;
      int i1 = 3;
      int i2 = 4;
      MyObject c1(i1);
      MyObject c2(i2);
    
      /* Storing object. */
      vec.push_back(c1);
    
      /* Assigning to it. */
      vec[0] = c2;
    
      /* Confirming that it's the correct object now. */
      for (const MyObject &it : vec)
        std::cout << it._i << std::endl;
    
      /* Modifying the value of its internal reference. */
      vec[0]._i.get() = 5;
      /* Confirming that the original int changed value indeed. */
      std::cout << "i2 == " << i2 << std::endl;
    
      return 0;
    }
    

    Caveat: Existing code may already contain direct assignments to the reference member (i.e. the member called _i in the code above). Those assignments were intended to change the value of the object the reference refers to. When replacing the reference with a std::reference_wrapper, all direct assignments _i = x must be replaced with _i.get() = x, otherwise the semantics of the program change entirely.

    (EDIT) If the references used are const-references const T&, a std::reference_wrapper<const T> can be used. Using the example above, the definition of MyObject then changes to this:

    struct MyObject
    {
      std::reference_wrapper<const int> _i;
      MyObject(const int &i):_i(i) {}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a list of objects, lets say of type Car, but
I would store objects in my std list on thread-safe way. I don't want
I know that you can store a bunch of Parent* objects in a vector.
i want to store a list of objects in session List<ContentQueueLog> inactiveContent = GetInActiveContent(this.userID,
I'm trying to store a list of generic objects in a generic list, but
I want to execute a function in a list of objects, and store the
My intention is to store a list of B objects in class A, but
i want to use a list which will store objects of some type (lets
I want to store a list of numbers (essentially, a set in mathematical terms)
I want to store a list of binary codes in a String[] array, such

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.