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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:38:29+00:00 2026-06-06T02:38:29+00:00

I am looking for a way to insert multiple objects of type A inside

  • 0

I am looking for a way to insert multiple objects of type A inside a container object, without making copies of each A object during insertion. One way would be to pass the A objects by reference to the container, but, unfortunately, as far as I’ve read, the STL containers only accept passing objects by value for insertions (for many good reasons). Normally, this would not be a problem, but in my case, I DO NOT want the copy constructor to be called and the original object to get destroyed, because A is a wrapper for a C library, with some C-style pointers to structs inside, which will get deleted along with the original object…

I only require a container that can return one of it’s objects, given a particular index, and store a certain number of items which is determined at runtime, so I thought that maybe I could write my own container class, but I have no idea how to do this properly.

Another approach would be to store pointers to A inside the container, but since I don’t have a lot of knowledge on this subject, what would be a proper way to insert pointers to objects in an STL container? For example this:

std::vector<A *> myVector;
for (unsigned int i = 0; i < n; ++i)
{
    A *myObj = new myObj();
    myVector.pushBack(myObj);
}

might work, but I’m not sure how to handle it properly and how to dispose of it in a clean way. Should I rely solely on the destructor of the class which contains myVector as a member to dispose of it? What happens if this destructor throws an exception while deleting one of the contained objects?

Also, some people suggest using stuff like shared_ptr or auto_ptr or unique_ptr, but I am getting confused with so many options. Which one would be the best choice for my scenario?

  • 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-06T02:38:31+00:00Added an answer on June 6, 2026 at 2:38 am

    In C++11, you can construct container elements in place using emplace functions, avoiding the costs and hassle of managing a container of pointers to allocated objects:

    std::vector<A> myVector;
    for (unsigned int i = 0; i < n; ++i)
    {
        myVector.emplace_back();
    }
    

    If the objects’ constructor takes arguments, then pass them to the emplace function, which will forward them.

    However, objects can only be stored in a vector if they are either copyable or movable, since they have to be moved when the vector’s storage is reallocated. You might consider making your objects movable, transferring ownership of the managed resources, or using a container like deque or list that doesn’t move objects as it grows.

    UPDATE: Since this won’t work on your compiler, the best option is probably std::unique_ptr – that has no overhead compared to a normal pointer, will automatically delete the objects when erased from the vector, and allows you to move ownership out of the vector if you want.

    If that’s not available, then std::shared_ptr (or std::tr1::shared_ptr or boost::shared_ptr, if that’s not available) will also give you automatic deletion, for a (probably small) cost in efficiency.

    Whatever you do, don’t try to store std::auto_ptr in a standard container. It’s destructive copying behaviour makes it easy to accidentally delete the objects when you don’t expect it.

    If none of these are available, then use a pointer as in your example, and make sure you remember to delete the objects once you’ve finished with them.

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

Sidebar

Related Questions

I'm looking for a way to insert a line of text above each line
I'm looking for the fastest way to insert multiple rows in a MySql table
I am looking the fastest way to insert many records at once (+1000) to
I am looking for a way to insert javascript code block to end of
I'm looking for a way to insert a new entry into a specific row
I'm looking for a way to map a simple insert stored procedure using NHibernate
I'm looking for way to write Javascript programs / scripts on desktop, not inside
I'm looking for a way to do multiple row inserts when I'm only inserting
I am looking for a way to insert a document into a CouchDB database
I'm looking for a way to insert a hidden input on submitting for 'Yes'

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.