向量的析构函数 [英] destructor for vector

查看:151
本文介绍了向量的析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


我有一个叫做容器的类,带有一个

的向量,指向一些基类作为成员。可以

有人帮我找到合适的析构函数

这堂课吗?


谢谢,Martijn Mulder

#include< vector.h>

#include< algorithm.h>

class base {};

class first: public base {};

class second:public base {};

class third:public base {};

class container
{

private:vector< base *> b;

public:container()

{

b.push_back(new first);

b.push_back(new second);

b.push_back(new third);

}

// ?? public:virtual~container(){for_each(b.begin(),b.end(),delete);}

};

int main(int argc, char ** argv)

{

容器c;

返回0;

}

Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class?

Thanks, Martijn Mulder
#include <vector.h>
#include <algorithm.h>
class base{};
class first:public base{};
class second:public base{};
class third:public base{};
class container
{
private:vector<base*>b;
public:container()
{
b.push_back(new first);
b.push_back(new second);
b.push_back(new third);
}
//?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}
};
int main(int argc,char**argv)
{
container c;
return 0;
}

推荐答案

2003年7月9日星期三01:12:04 +0200,特工Mulder写道:
On Wed, 09 Jul 2003 01:12:04 +0200, Agent Mulder wrote:
嗨组,

我有一个名为container的类,带有一些指向某些基类的成员的向量。可以有人帮我找到这门课的正确的析构函数吗?
[snip] // ?? public:virtual~container(){for_each(b.begin(),b.end(),delete);}
Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class? [snip] //?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}



[snip]


您可以创建一个符合您需要的仿函数:

template< class T>

struct doDelete {

void operator( )(T * target){删除目标; }

};


virtual~container(){for_each(b.begin(),b.end(),delete< base>()) ; }

-

Blake Kaplan


战争并不能确定谁是对的,但是谁是谁左边。


[snip]

You could create a functor which does what you need:
template <class T>
struct doDelete {
void operator()(T *target) { delete target; }
};

virtual ~container() { for_each(b.begin(), b.end(), delete<base>()); }
--
Blake Kaplan

"War doesn''t determine who''s right, but who''s left."


" Agent Mulder" < MB ******************* @ home.nl>在消息中写道

news:be ********** @ news2.tilbu1.nb.home.nl ...
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:be**********@news2.tilbu1.nb.home.nl...
嗨组,

我有一个名为container的类,它带有一个指向某个基类的指针作为成员。有人可以帮我找到合适的析构函数吗?

谢谢,Martijn Mulder

#include< vector.h>
#include< algorithm.h>
class base {};
class first:public base {};
class second:public base {};
class third:公共基地{};
类容器
{
私人:vector< base *> b;
public:container()
{
b。 push_back(新的第一个);
b.push_back(新的第二个);
b.push_back(新的第三个);
}
// ?? public:virtual~container(){for_each(b.begin(),b.end(),delete);}
};
int main(int argc,char ** argv)
{
容器c;
返回0;
}
Hi group,

I have a class called container with a vector of
pointers to some base class as member. Can
someone help me find the right destructor for
this class?

Thanks, Martijn Mulder
#include <vector.h>
#include <algorithm.h>
class base{};
class first:public base{};
class second:public base{};
class third:public base{};
class container
{
private:vector<base*>b;
public:container()
{
b.push_back(new first);
b.push_back(new second);
b.push_back(new third);
}
//?? public:virtual ~container(){for_each(b.begin(),b.end(),delete);}
};
int main(int argc,char**argv)
{
container c;
return 0;
}




#include" boost / smart_ptr.hpp" ; //参见 www.boost.org


类容器

{


public:

typedef boost :: shared_ptr< base> t_pbase;

私人:

vector< t_pbase> b;

public:

container()

{

b.push_back(t_pbase(new first));

b.push_back(t_pbase(new second));

b.push_back(t_pbase(new third));

}

//无需析构函数,复制构造函数或赋值运算符


需要析构函数,赋值运算符或副本的类

构造函数几乎总是需要这三个。在这种情况下,只需在您的类中添加一个

析构函数就不够好 - 任何复制此类

对象的尝试最终都会导致相同的指针被删除

两次。所以你必须添加一个赋值运算符和一个副本

构造函数,记得检查x = x blah blah blah


我建议你放轻松关于你自己并使用一个聪明的引用计数。如上所示,
指针。它具有您想要的复制语义,为您管理

内存,并且在大多数情况下都像常规指针一样。


-

Cy
http://home.rochester.rr.com / cyhome /


John Harrison写道:
John Harrison wrote:

这就是为什么Cy''应该考虑智能指针,因为它们删除了编写析构函数,复制构造函数和赋值操作符的需要,从而简化了您的任务。

john

That''s why Cy''s smart pointers should be considered because they remove
the need to write the destructor, copy constructor and assignment
operator, thereby simplifying your task.

john




我从未听说过这些Cy智能指针,但它们听起来非常有用。

它们是如何使用的?任何可以向我们展示的代码片段?

谢谢,

pete



I have never heard of these Cy smart pointers, but they sound very useful.
How are they used? Any code snippets that could show us?
Thanks,
pete


这篇关于向量的析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆