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

问题描述

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

推荐答案




删除vptr;


就足够了。


TB



delete vptr;

will be sufficient.

TB






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

你应该在完成后删除它:


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

销毁载体中包含的对象。

-

Clark S. Cox,III




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


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

08-23 01:02
查看更多