新建/删除“覆盖”与“过载”相比

发布于 2024-09-19 14:06:45 字数 314 浏览 3 评论 0原文

我一直认为...

  • 覆盖意味着在基类中重新实现一个函数(相同的签名),而
  • 重载意味着实现一个具有相同名称但不同签名的函数

...并且得到困惑是因为有时人们根本不关心差异。

关于新建/删除:它们是否超载或被覆盖?

一个想法:

  • 在类中实现 new/delete 运算符 = 重载
  • 重新实现全局 new/delete = 覆盖 有

任何更正/建议/反对吗? 并随意将问题标记为“吹毛求疵”......

I always thought...

  • overriding means reimplementing a function (same signature) in a base class whereas
  • overloading means implementing a function with same name but different signature

... and got confused because sometimes people just don't care about the difference.

Concerning new/delete: Are they overloaded or overridden?

An idea:

  • implementing new/delete operator in a Class = overload
  • reimplementing global new/delete = override

Any corrections/suggestions/objections?
And feel free to tag the question "hairsplitting"...

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

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

发布评论

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

评论(1

习惯成性 2024-09-26 14:07:25

对于全局operator newoperator delete来说,它实际上既不是重载也不是重写。程序可以用自己的定义替换默认的、实现提供的定义。 C++ 标准规定 (§3.7.3/2):

该库提供了全局分配和释放函数的默认定义。一些全局分配和释放函数是可以替换的(18.4.1)。 C++ 程序最多应提供一个可替换分配或释放函数的定义。任何此类函数定义都会替换库中提供的默认版本 (17.4.3.4)。

对于特定于类的operator newoperator delete,运算符重载

For the global operator new and operator delete, it's actually neither overloading nor overriding. A program is permitted to replace the default, implementation-provided definitions with its own definitions. The C++ standard says (§3.7.3/2):

The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.4.1). A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the default version provided in the library (17.4.3.4).

For a class-specific operator new or operator delete, the operators are overloaded.

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