重载、覆盖和替换新/删除有哪些限制?

发布于 2024-12-25 05:19:45 字数 482 浏览 1 评论 0原文

据我所知,有 3 种通用方法来修改 new 的行为并在 C++ 中删除:

  • 替换默认的 new/delete 和 new[]/delete[]
  • 覆盖重载放置版本(覆盖传递给它的内存位置的版本,在创建传递其他类型或数量的参数的版本时重载)
  • 重载类特定版本。

对新建/删除行为执行这些修改有哪些限制?

特别是 new 和 delete 可以使用的签名是否有限制?

如果任何替换版本必须具有相同的签名,这是有意义的(否则它们不会被替换或会破坏其他代码,例如 STL),但是是否允许全局放置或类特定版本返回智能指针或某些例如自定义手柄?

I understand that there are 3 general ways to modify the behaviour of new and delete in C++:

  • Replacing the default new/delete and new[]/delete[]
  • Overriding or overloading the placement versions (overriding the one with a memory location passed to it, overloading when creating versions which pass other types or numbers of arguments)
  • Overloading class specific versions.

What are the restrictions for performing these modifications to the behaviour of new/delete?

In particular are there limitations on the signatures that new and delete can be used with?

It makes sense if any replacement versions must have the same signature (otherwise they wouldn't be replacement or would break other code, like the STL for example), but is it permissible to have global placement or class specific versions return smart pointers or some custom handle for example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

玻璃人 2025-01-01 05:19:45

首先,不要将 new/delete 表达式operator new() function 混淆。

表达式是执行构造和破坏的语言构造。该运算符是执行内存分配(释放)的普通函数。

仅默认运算符(operator new(size_t)operator delete(void *) 可以与默认的 newdelete 一起使用 表达式。所有其他形式统称为“放置”形式,对于这些形式,您只能使用 new,但您必须通过调用析构函数来手动销毁对象。到目前为止,有限和专门的需求。最有用的放置形式是全局放置新,::new (addr) T,但其行为甚至无法更改(这大概就是为什么它是唯一流行的

所有)。 new 运算符必须返回 void *。这些分配函数比您想象的要低级得多,因此基本上您“会知道何时需要弄乱它们”

。 C++ 将对象构造和内存分配的概念分开。可以做的是为后者提供替代实现。

First off, don't confuse the new/delete expression with the operator new() function.

The expression is a language construct that performs construction and destruction. The operator is an ordinary function that performs memory (de)allocation.

Only the default operators (operator new(size_t) and operator delete(void *) can be used with the default new and delete expressions. All other forms are summarily called "placement" forms, and for those you can only use new, but you have to destroy objects manually by invoking the destructor. Placement forms are of rather limited and specialised need. By far the most useful placement form is global placement-new, ::new (addr) T, but the behavior of that cannot even be changed (which is presumably why it's the only popular one).

All new operators must return void *. These allocation functions are far more low-level than you might appreciate, so basically you "will know when you need to mess with them".

To repeat: C++ separates the notions of object construction and memory allocation. All you can do is provide alternative implementations for the latter.

画离情绘悲伤 2025-01-01 05:19:45

当您在类中重载 new 和 delete 时,您实际上是在修改该类分配和释放内存的方式,并要求它为您提供这种控制权。

当类想要使用某种类型的池来分配其实例时(出于优化或跟踪目的),可以执行此操作。

与几乎任何运算符重载一样,限制是您可以传递的参数列表,以及它期望遵守的行为。

When you overload new and delete within a class you are effectively modifying the way the memory is allocated and released for the class, asking for it to give you this control.

This may be done when a class wants to use some kind of pool to allocate its instances, either for optimisation or for tracking purposes.

Restrictions, as with pretty much any operator overload, is the parameter list you may pass, and the behaviour it is expected to adhere to.

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