我们如何调用“删除这个”? ”在常量成员函数中?

发布于 2024-10-19 18:56:06 字数 418 浏览 1 评论 0原文

我看到的代码片段如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唯憾梦倾城 2024-10-26 18:56:06

其工作原理与此工作原理相同:

const int* upn = new int();
delete upn;

您可以删除指向 const 限定对象的指针。成员函数上的 const 限定仅意味着 this 具有类型 const UPNumber*

That works for the same reason that this work:

const int* upn = new int();
delete upn;

You can delete a pointer to a const-qualified object. The const-qualification on the member function just means that this has a type const UPNumber*.

辞旧 2024-10-26 18:56:06

应用于方法的 const 限定符的作用是使传递给它的 this 成为一个 const 指针;特别是,在您的情况下,它将是一个 const UPNumber *

不过,这对于 delete 来说不是问题:实际上,您可以在 const 指针上使用 delete,而无需强制转换任何内容,如 § 中指定的那样5.3.5¶2:

[注意:指向 const 类型的指针可以是删除表达式的操作数;在将指针表达式用作删除表达式的操作数之前,无需放弃指针表达式的常量性 (5.2.11)。 ]

请注意,在标准完成之前,已经有很多关于这是否是一个好主意的讨论,因此一些预标准编译器在尝试删除时会发出错误 const 指针。

允许这种行为背后的想法是,否则您将无法在不使用 const_cast 的情况下删除 const 对象;有关详细信息,请参阅此问题

The const qualifier applied to a method have the effect of making the this passed to it a const pointer; in particular, in your case it will be a const UPNumber *.

Still, this is not a problem for delete: actually you can use delete on a const pointer without having to cast anything, as specified at §5.3.5 ¶2:

[Note: a pointer to a const type can be the operand of a delete-expression; it is not necessary to cast away the constness (5.2.11) of the pointer expression before it is used as the operand of the delete-expression. ]

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 a const_cast; see this question for more info.

驱逐舰岛风号 2024-10-26 18:56:06

您从哪里得知我们正在修改?事实上,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 is const 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 by delete.

So, there are no problems with that application of delete in your code.

俏︾媚 2024-10-26 18:56:06

因为调用删除此技术上是可行的并且已定义如果您小心,“自杀”方法的常量性技术上毫无意义,因为在调用后不得触摸该对象。

这就是为什么使用 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 calling delete 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文