我们如何调用“删除这个”? ”在常量成员函数中?
我看到的代码片段如下:
class UPNumber {
public:
UPNumber();
UPNumber(int initValue);
...
// pseudo-destructor (a const member function, because
// even const objects may be destroyed)
void destroy() const { delete this; } // why this line is correct???
...
private:
~UPNumber();
};
首先,我确信上面的类定义是正确的。 这是我的问题,为什么我们可以像上面那样定义函数“destroy”? 问题的原因是为什么我们可以在 const 成员函数中修改“this”?
I saw the code snippet as follows:
class UPNumber {
public:
UPNumber();
UPNumber(int initValue);
...
// pseudo-destructor (a const member function, because
// even const objects may be destroyed)
void destroy() const { delete this; } // why this line is correct???
...
private:
~UPNumber();
};
First, I am sure that above class definition is correct.
Here is my question, why we can define the function 'destroy' as above?
The reason being asking is that why we can modify 'this' in a const-member function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其工作原理与此工作原理相同:
您可以
删除
指向 const 限定对象的指针。成员函数上的 const 限定仅意味着this
具有类型const UPNumber*
。That works for the same reason that this work:
You can
delete
a pointer to a const-qualified object. The const-qualification on the member function just means thatthis
has a typeconst UPNumber*
.应用于方法的
const
限定符的作用是使传递给它的this
成为一个const
指针;特别是,在您的情况下,它将是一个const UPNumber *
。不过,这对于
delete
来说不是问题:实际上,您可以在const
指针上使用delete
,而无需强制转换任何内容,如 § 中指定的那样5.3.5¶2:请注意,在标准完成之前,已经有很多关于这是否是一个好主意的讨论,因此一些预标准编译器在尝试
删除
时会发出错误const
指针。允许这种行为背后的想法是,否则您将无法在不使用
const_cast
的情况下删除
const
对象;有关详细信息,请参阅此问题。The
const
qualifier applied to a method have the effect of making thethis
passed to it aconst
pointer; in particular, in your case it will be aconst UPNumber *
.Still, this is not a problem for
delete
: actually you can usedelete
on aconst
pointer without having to cast anything, as specified at §5.3.5 ¶2:Notice that, before the standard was completed, there have been many discussion about whether this was or wasn't a good idea, so some pre-standard compilers will issue an error when trying to
delete
const
pointers.The idea behind allowing this behavior is that otherwise you would have no way to
delete
const
objects without using aconst_cast
; see this question for more info.您从哪里得知我们正在修改
此
?事实上,this
不是左值。无论成员函数是否为 const,它都不能被修改。将删除表达式应用于指针(任何指针,而不仅仅是
this
)无论如何都不会被视为对该指针的修改。此外,delete-expression 的参数被视为右值,这意味着它不可能被delete
修改。因此,在代码中应用
delete
没有问题。Where did you get the idea that we are modifying
this
? In fact,this
is not an lvalue. It It cannot ever be modified, regardless of whether the member function isconst
or not.Applying delete-expression to a pointer (any pointer, not just
this
) is not in any way considered a modification of that pointer. Moreover, the argument of delete-expression is treated as rvalue, meaning that it cannot possibly be modified bydelete
.So, there are no problems with that application of
delete
in your code.因为调用
删除此
技术上是可行的并且已定义如果您小心,“自杀”方法的常量性技术上毫无意义,因为在调用后不得触摸该对象。这就是为什么使用
const
方法调用delete this
是语义不正确的原因。一个有趣的观点带来了 史蒂夫·杰西普的评论。运行析构函数不会使对象保持不变,因此应用常量概念是有问题的。
As it is technically possible and defined to call
delete this
provided that you are careful, the constness of the "suicide" method is technically pointless, since the object must not be touched after the call.That's why it is semantically incorrect to have a
const
method callingdelete this
.An interesting point brought Steve Jessep's comment. Running the destructor doesn't keep an object unchanged, so applying the concept of constness is questionable.