一个解构主义者的问题 [英] a deconstructor question
问题描述
亲爱的,
我有点困惑。
//这个类的对象是部分填充的双打数组
类PFArray
{
public:
...
~PFArray() ;
私人:
double * a; //一组双打
..
};
PFArray ::〜PFArray()
{
删除[] a;
}
代码double * a在PFArray类的私有部分中没有
NECESSARILY告诉a a是一个指向数组的指针(它可以指向一个指针
到一个double,对吗?) 。为什么解构者使用删除[] a"?如果
而是指向double的指针,则删除a应该用吗?
Dear All,
I am a little confused.
//Objects of this class are partially filled arrays of doubles
class PFArray
{
public:
...
~PFArray();
private:
double *a; //for an array of doubles
..
};
PFArray::~PFArray()
{
delete [] a;
}
The code "double *a" in the private section of class PFArray doesn''t
NECESSARILY tell that a is a pointer to an array (it coulde be a pointer
to a double, right?). Why the deconstructor use " delete [] a "? If a
instead is pointer to a double, then "delete a" should be used?
推荐答案
" Xiaoshen Li" < XL ** @ gmu.edu>在留言中写道
news:dp *********** @ osf1.gmu.edu ...
"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp***********@osf1.gmu.edu...
亲爱的所有人,
<我有点困惑。
//这个类的对象是部分填充的双打数组
class PFArray
{
公开:
......
~PFArray();
私人:
double * a; //为一组双打
..
};
PFArray ::〜PFArray()
{
删除[] a;
}
代码double * a在类PFArray的私有部分中,并不是必须告诉a a是一个指向数组的指针(它可以指向一个双指针,对吗?)。
它是*指向double类型的指针。指向数组的指针看起来像
例如:
double(* a)[size];
为什么解构器使用删除[] a"?
如果指针的值已经返回''new []',那么它应该只使用''delete []'''
''''' '或者是NULL(0)。
否则行为是未定义的。
如果a是指向double的指针,则删除&应该使用?
Dear All,
I am a little confused.
//Objects of this class are partially filled arrays of doubles
class PFArray
{
public:
...
~PFArray();
private:
double *a; //for an array of doubles
..
};
PFArray::~PFArray()
{
delete [] a;
}
The code "double *a" in the private section of class PFArray doesn''t
NECESSARILY tell that a is a pointer to an array (it coulde be a pointer
to a double, right?).
It *is* a pointer to type double. A pointer to an array would look like
e.g:
double (*a)[size];
Why the deconstructor use " delete [] a "?
It should only use ''delete[]'' if the value of the pointer
''a'' was returned ''new[]'' or is NULL (0).
Otherwise the behavior is undefined.
If a instead is pointer to a double, then "delete a" should be used?
只有''新''(不是''[]'')返回''a'的值时。
a =新的双倍; //分配单个类型的双重对象
删除a; //释放单一类型的双重对象
//删除[] a; //未定义的行为。
a = new double [5]; //分配五个类型双重对象的数组
delete [] a; //释放五个双打数组//
删除a; //未定义的行为。
双d;
a =& d; //''''的值不是来自''new''或''new []''
delete d; //未定义的行为
delete [] d; //未定义的行为
您正在阅读哪些C ++书籍?
-Mike
Only if ''a''s value was returned by ''new'' (not ''[]'').
a = new double; // allocates a single type double object
delete a; // deallocate the single type double object
// delete[] a; // undefined behavior.
a = new double[5]; // allocates array of five type double objects
delete[] a; // deallocate the array of five doubles//
delete a; // undefined behavior.
double d;
a = &d; // ''a''s value not from ''new'' or ''new[]''
delete d; // undefined behavior
delete[] d; // undefined behavior
Which C++ book(s) are you reading?
-Mike
感谢您的帮助。但我不确定你是对的。
三年前我参加了C ++课程。从那时起,我没有使用它。
昨天,我看到教授传递的课程代码:
class Person >
{
public:
..
~Person();
private:
char * name;
};
Person ::〜Person()
{
删除[]名称;
}
由于名称可能指向单个char或char数组,为什么
析构函数使用" delete [] name"?
再次感谢你。
Thank you for the help. But I am not sure you are right.
I took C++ class three years ago. Since then, I didn''t use it.
Yesterday, I saw the code in the class passed by the professor:
class Person
{
public:
..
~Person();
private:
char *name;
};
Person::~Person()
{
delete [] name;
}
Since name could be pointing a single char or a char array, why the
destructor uses "delete [] name"?
Thank you again.
Xiaoshen Li写道:
Xiaoshen Li wrote:
谢谢你的帮助。但我不确定你是对的。
他是。
我三年前参加过C ++课程。从那以后,我没有使用它。
昨天,我看到教授通过的课程代码:
班级人员
{
公开:
..
〜人();
私人:
char * name;
};
Person :: ~Person()
{
删除[]名称;
}
由于name可能指向一个char或char数组,为什么
析构函数使用"删除[]名称"?
Thank you for the help. But I am not sure you are right.
He is.
I took C++ class three years ago. Since then, I didn''t use it.
Yesterday, I saw the code in the class passed by the professor:
class Person
{
public:
..
~Person();
private:
char *name;
};
Person::~Person()
{
delete [] name;
}
Since name could be pointing a single char or a char array, why the
destructor uses "delete [] name"?
规则是 - 如果您使用new []分配,则使用delete []解除分配
。如果您使用new进行分配,则需要使用
delete取消分配。
因此代码由the professor指定。只有当存储
为''name''分配new [](这很可能是
的情况下)时才会很好。
HTH,
- J.
The rule is -- if you allocate with new[], you deallocate
with delete[]. If you allocate with new, you deallocate with
delete.
Therefore the code by "the professor" is fine only if storage
for ''name'' is allocated with new[] (which is most probably
the case).
HTH,
- J.
这篇关于一个解构主义者的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!