重写运算符

发布于 2024-10-02 15:22:51 字数 214 浏览 5 评论 0原文

关于构造函数重写的任何人..

我的类中有这个

void  operator delete(void*) {}

void  operator delete(void* p, void*) {}

..看起来像重载(相同的函数名称和返回类型但不同的参数列表)但它的重写..它如何重写..

任何人都可以解释我这两行函数。

anybody me about constructor overriding..

i have this

void  operator delete(void*) {}

void  operator delete(void* p, void*) {}

in my class.. that looks like overloading(same function name and return type but different parameter list) but its overriding .. how its overriding..

anyone an explain me these two lines function.

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

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

发布评论

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

评论(3

情归归情 2024-10-09 15:22:51
void  operator delete(void*) {}
void  operator delete(void* p, void*) {}

这些是自定义释放函数。通过删除表达式调用释放函数。例如,

YourClass* p = new YourClass();    // Allocates memory & calls constructor
// ... whatever, then
delete p;                          // Calls destructor & deallocates memory

对于您的类,上面最后一行中的 delete 表达式将首先调用析构函数,然后它将调用该类定义的单个 void* 参数释放函数,即您的两个函数中的第一个,如果可以访问该释放。

但是,释放函数可能被声明为 privateprotected,以使其不可访问。在第一种情况下,类自身代码之外的删除表达式将无法编译(无法访问释放函数)。如果是这样,那么这可能就是重点——或者,如果根本没有意义,也不要感到惊讶。

顺便说一句,请查看本教程。这显然是网上最不糟糕的免费 C++ 介绍。 Bruce Eckel 的电子书 “Thinking in C++” 也是免费的,但它有一些错误和错误信息(虽然以前是相反的,cplusplus.com 上的教程曾经非常糟糕)。

干杯&呵呵,

void  operator delete(void*) {}
void  operator delete(void* p, void*) {}

These are custom deallocation functions. A deallocation function is called via a deleteexpression. E.g.

YourClass* p = new YourClass();    // Allocates memory & calls constructor
// ... whatever, then
delete p;                          // Calls destructor & deallocates memory

For your class the delete expression in the last line above would first call the destructor, and then it would call the single void* argument deallocation function that the class defines, the first of your two functions, if that deallocation is accessible.

However, it might be that the deallocation function is declared as private or protected, for the purpose of making it inaccessible. In the first case a delete expression outside the class' own code won't compile (inaccessible deallocation function). And if so then that may be the whole point -- or, don't be surprised if there's no point at all.

By the way, have a look at this tutorial. It's apparently the least bad free introduction to C++ on the net. Bruce Eckel's e-book "Thinking in C++" is also free, but it has some errors and misinformation (it used to be the other way around though, the tutorial at cplusplus.com used to be very bad, once).

Cheers & hth.,

不寐倦长更 2024-10-09 15:22:51

他们正在重载删除运算符,第二个是使用“放置删除”。放置 new/delete 是一种 hack,这样您就可以让 C++ 构造函数在您提供的内存之上初始化类(而不是 ::new),并且可能还可以使用您在此处看到的其他参数。谷歌可以向你提供血淋淋的细节。

They're overloading the delete operator, and the second one is using "placement delete". Placement new/delete are a hack so that you can have C++ constructors initialize the class on top of memory you provide (instead of ::new), and possibly with additional arguments like you see here. Google can fill you in with the gory details.

小伙你站住 2024-10-09 15:22:51

这些是运算符删除重载,并在对象上调用delete 时调用。从 3.7.3.2(解除分配函数)开始:

每个释放函数应返回 void 并且其第一个参数应为 void*。解除分配函数
可以有多个参数。 如果类T有一个名为operator的成员释放函数
如果仅使用一个参数进行删除,则该函数是一种常见的(非放置)释放函数
。如果
类T没有声明这样的运算符delete,但声明了成员释放函数
具有两个参数的命名运算符删除,其中第二个参数的类型为 std::size_t
(18.1),那么这个函数就是一个通常的释放函数。

在 12.5 免费商店中:

当执行删除表达式时,应使用地址调用选定的释放函数
要回收的存储块作为其第一个参数以及(如果使用两参数样式)的大小
该块作为其第二个参数

我不确定你的第二个函数是否可以被调用,因为它不是“通常的释放函数”。

These are operator delete overloading and are called when delete is invoked on the object. From 3.7.3.2 (Deallocation functions) :

Each deallocation function shall return void and its first parameter shall be void*. A deallocation function
can have more than one parameter. If a class T has a member deallocation function named operator
delete with exactly one parameter, then that function is a usual (non-placement) deallocation function
. If
class T does not declare such an operator delete but does declare a member deallocation function
named operator delete with exactly two parameters, the second of which has type std::size_t
(18.1), then this function is a usual deallocation function.

And in 12.5 Free store :

When a delete-expression is executed, the selected deallocation function shall be called with the address of
the block of storage to be reclaimed as its first argument and (if the two-parameter style is used) the size of
the block as its second argument
.

I'm not sure your second function can ever be called as it is not a 'usual deallocation function'.

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