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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:32:31+00:00 2026-05-25T15:32:31+00:00

this is a question about which class does a free on shared pointers. So

  • 0

this is a question about which class does a free on shared pointers.

So I have a class hierarchy, base and derivedA and derivedB and derivedC all from base. Base has some virtual functions.

I have a class, Holder, it has a collection that holds instances of derivedA, derivedB and derivedC. So an instance of Holder is created, instances of the derived classes are dynamically created and add their pointers to the containers. I add the pointers so I can iterate through the container and call base->virtualFunction. I new the objects because otherwise an instance created on the stack is destroyed when it goes out of scope.

class Base;
class DerivedA : public base;
class DerivedB : public base;
class DerivedC : public base;


class Holder {
    std::vector<Base*> collection;
    void add(Base* base);
}

Holder holder;
DerivedA* da = new DerivedA;
DerivedB* db = new DerivedB;

holder.add(da);
holder.add(db);

who is to call delete on da and db?

Any other way to design this for the deletion concern goes away?

Thanks
Reza

My question is first off, which class should be responsible to free the dynamically created the derived classes? The class the contains the list

  • 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-25T15:32:32+00:00Added an answer on May 25, 2026 at 3:32 pm

    The simpler way ? Change your vector to become aware of ownership.

    boost::ptr_vector<Base>
    

    is such a vector, especially suited for polymorphic data:

    • deep-copy through clone methods (just implement virtual Base* clone() const;)
    • automatic memory management
    • sugar coating on dereferencing iterator (yields a Base& instead of a Base*)

    In C++11, another alternative could be std::vector< std::unique_ptr<Base> >.

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

Sidebar

Related Questions

I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
I have a question about this question . I posted a reply there but
This question is about App domains and Sessions. Is it possible to have IIS
I have a question about the way in which the output is displayed. <?php
I already asked this question and I got additional question about using class I
I have this question about c# language's dynamic binding behavior. Consider the following object
I have a question about the List class. From what I understand List is
This question is about dealing with testing of classes which mix in non-interface traits,
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages

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.