存储在 ptr_vector 中的派生类未被破坏

发布于 2024-09-28 03:07:45 字数 1452 浏览 0 评论 0 原文

试图找到使用 ptr_vector 存储、访问和释放对象的最佳方法,特别是当存储的对象是从其他对象继承时(ptr_vector 不应该有任何对象切片问题)。 但当运行下面的程序时,令人惊讶的是派生类没有被破坏。有人知道为什么吗?

#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

程序的输出是:

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

为什么 B0 没有被破坏?物体是否被切片?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-10-05 03:07:45

基类的析构函数不是虚拟的:

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

应该是:

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

为什么?请参阅什么时候析构函数应该是虚拟的?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文