指向矢量?我应该删除它还是清除它? [英] pointer to a vector? shall i delete it or clear it?

查看:87
本文介绍了指向矢量?我应该删除它还是清除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


在我的程序中,我使用指向矢量的指针

vector< XYZ> * vptr =新向量< XYZ> ;;


以及XYZ类有一个char *作为其成员之一。我创建了

所有副本XYZ的构造函数,赋值运算符和析构函数(Big

三)。


我的问题是如何释放vptr所拥有的内存?

我应该删除vptr还是

vptr-> Clear()就足够了?


(在两种情况下都是XYZ的析构函数)对象被称为..)


问候,

Yogesh Joshi

Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)

regards,
Yogesh Joshi

推荐答案

yp*********@indiatimes.com sade:
Hello All,

在我的程序中,我使用指向矢量的指针
矢量< XYZ> * vptr = new vector< XYZ> ;;

并且XYZ类还有一个char *作为其成员之一。我创建了所有的复制构造函数,赋值运算符和析构函数(The对于XYZ来说很大。

我的问题是如何释放vptr所拥有的内存?
我应该删除vptr或
vptr-> Clear( )是不够的?

(在两种情况下都会调用XYZ对象的析构函数。)

问候,
Yogesh Joshi
Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)

regards,
Yogesh Joshi




删除vptr;


就足够了。


TB



delete vptr;

will be sufficient.

TB


如果你分配内存,你也应该删除它=>删除vptr。


问候,斯蒂芬

If you allocate memory you also should delete it => delete vptr.

Regards, Stephan


On 2006-01-12 08:47:54 -0500 , yp*********@indiatimes.com 说:
On 2006-01-12 08:47:54 -0500, yp*********@indiatimes.com said:
Hello All,

在我的程序中,我使用指向矢量的指针
矢量< XYZ> * vptr = new vector< XYZ> ;;

并且XYZ类还有一个char *作为其成员之一。我创建了所有的复制构造函数,赋值运算符和析构函数(The对于XYZ来说很大。

我的问题是如何释放vptr所拥有的内存?
我应该删除vptr或
vptr-> Clear( )是不够的?

(在两种情况下都会调用XYZ对象的析构函数。)
Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)




就像任何其他对象一样你已经通过新的操作员分配了,

你应该在完成后删除它:


删除vptr;
不会删除向量本身;它只会

销毁载体中包含的对象。

-

Clark S. Cox,III
cl ******* @ gmail.com



Just like any other object that you''ve allocated via the new operator,
you should delete it when you are finished with it:

delete vptr;

calling vptr-clear() will not delete the vector itself; it will merely
destroy the objects contained within the vector.
--
Clark S. Cox, III
cl*******@gmail.com


这篇关于指向矢量?我应该删除它还是清除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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