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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:33:49+00:00 2026-05-31T06:33:49+00:00

I have a collection of classes all derived from a common base. I need

  • 0

I have a collection of classes all derived from a common base. I need a collection (probably a list) that will hold instances of derived classes of various types. Class operations will call virtual methods on the instances in the collection. Of course, I can’t use list<Base> because holding the base class by value will slice the derived classes.

The obvious solution is to use list<Base*> and wrap it in a class with a copy constructor, a destructor, and so on. The base class would have a virtual duplicate function that is overloaded in each derived class to return a pointer to a copy-constructed new instance of that derived class. The destructor has to traverse the list and delete each item.

If there’s a Boost way to do this, that’s fine. I’m using Boost already. It seems to me that using Boost’s shared pointers would be the best way. There would be overhead in managing the shared reference counts, but that’s got to be less expensive than the allocate/copy required in the pointer case. However, this will mean that copies of the collection will result in the same instances and changing one copy will change the other.

I don’t yet completely know how this code is going to actually be used. So I’m not sure if the shared copy semantics are going to be a problem. I don’t think copies are going to be common, they just need to be handled sanely.

Is there another way?

  • 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-31T06:33:51+00:00Added an answer on May 31, 2026 at 6:33 am

    Say hello to Boost.PointerContainer. By default, these offer deep-copy semantics when you copy the containers (this is, however, customisable). Note that you need to implement a free new_clone method for your abstract base class in the appropriate namespace.

    Also, as a side note, please use std::vector as your first-choice container.

    Another side note, don’t wrap the container<Base*>, but rather wrap the Base* in, you guessed it, a smart pointer. C++11 offers unique_ptr if you don’t want shared ownership. unique_ptr only allows moving it, you can’t copy it.

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

Sidebar

Related Questions

I have a collection of classes that inherit from an abstract class I created.
I have a collection of classes that I want to serialize out to an
A lot of the collection classes in .Net (i.e., List<T>, Dictionary<TKey, TValue>) have an
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
I have Collection List<Car> . How to compare each item from this collection with
All of the collection classes have two versions - mutable and immutable, such as
vtable is an overhead in all base/derived classes when a base class has a
I have a need for specialized collection classes. Let's call them FooItems and BarItems.
I have a collection of classes defined from UIView. @interface { UIView *thisView UIView
I have a Base Class, called primitive Graphics. derived from this class are several

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.