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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:16:55+00:00 2026-05-17T19:16:55+00:00

Was trying to find the best way to use ptr_vector to store, access and

  • 0

Was trying to find the best way to use ptr_vector to store, access and release objects, especially when the stored object is inherited from other (ptr_vector should not have any issues with object slicing).
But when running the below program, surprisingly the derived class isn’t being destructed. Anyone know why?

#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/foreach.hpp>
using namespace std;

class A
{
 public:
 int id;
 A() {cout<<"Constructed A()"<<endl;}
 A(int i):id(i) {cout<<"Constructed A"<<i<<endl;}
 ~A() {cout<<"* Destructed A"<<id<<endl;}
};
class B:public A
{
 public:
 int i;
 B() {cout<<"Constructed B"<<endl;}
 B(int ii):i(ii) {id=ii;cout<<"Constructed B"<<i<<endl;}
 ~B() {cout<<"* Destructed B"<<i<<endl;}
};

class zoo
{
 boost::ptr_vector<A> the_animals;
public:
 void addAnimal(A* a) {the_animals.push_back( a );}
 void removeAnimal(int id) {the_animals.release(the_animals.begin()+id); }
 void removeOwnership(int id) {the_animals.release(the_animals.begin()+id).release();}
};

int main()
{
 zoo z;
 z.addAnimal( new B(0) );
 //delete abc;z.addAnimal(abc);//doing this will cause heap corruption
 B* lion=new B(1);
 z.addAnimal(lion);
 z.removeOwnership(1);
        delete lion;
 z.removeAnimal(0);
}//main

The output for the program is:

Constructed A()
Constructed B0
Constructed A()
Constructed B1
* Destructed B1
* Destructed A1
* Destructed A0

Why isn’t B0 being destructed? Did the object get sliced?

  • 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-17T19:16:56+00:00Added an answer on May 17, 2026 at 7:16 pm

    The destructor of the base class is not virtual:

     ~A() {cout<<"* Destructed A"<<id<<endl;}
    

    Should be:

     virtual ~A() {cout<<"* Destructed A"<<id<<endl;}
    

    Why ? See When should your destructor be virtual?

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

Sidebar

Related Questions

I'm trying to find the best way to use Java libraries that were installed
I'm trying to find the best way to load a source table from MySQL
I'm trying to find the best way to update an object which could be
I'm trying to find the best way to use union with linq. currently I
I'm trying to find the best way to retrieve fields from a table, depending
I'm trying to find out the best way to handle transactions at object level
I'm trying to find the best way to uncouple messageboxes from my logic so
Trying to find the best way of create an overlap/overlay layer to fill the
I am trying to find the best way to implement Model using Zend Framework
I'm trying to find the best way to design my application and precisely my

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.