重写运算符
关于构造函数重写的任何人..
我的类中有这个
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些是自定义释放函数。通过删除表达式调用释放函数。例如,
对于您的类,上面最后一行中的
delete
表达式将首先调用析构函数,然后它将调用该类定义的单个void*
参数释放函数,即您的两个函数中的第一个,如果可以访问该释放。但是,释放函数可能被声明为
private
或protected
,以使其不可访问。在第一种情况下,类自身代码之外的删除表达式将无法编译(无法访问释放函数)。如果是这样,那么这可能就是重点——或者,如果根本没有意义,也不要感到惊讶。顺便说一句,请查看本教程。这显然是网上最不糟糕的免费 C++ 介绍。 Bruce Eckel 的电子书 “Thinking in C++” 也是免费的,但它有一些错误和错误信息(虽然以前是相反的,cplusplus.com 上的教程曾经非常糟糕)。
干杯&呵呵,
These are custom deallocation functions. A deallocation function is called via a
delete
expression. E.g.For your class the
delete
expression in the last line above would first call the destructor, and then it would call the singlevoid*
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
orprotected
, for the purpose of making it inaccessible. In the first case adelete
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.,
他们正在重载删除运算符,第二个是使用“放置删除”。放置 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.
这些是运算符删除重载,并在对象上调用
delete
时调用。从 3.7.3.2(解除分配函数)开始:在 12.5 免费商店中:
我不确定你的第二个函数是否可以被调用,因为它不是“通常的释放函数”。
These are operator delete overloading and are called when
delete
is invoked on the object. From 3.7.3.2 (Deallocation functions) :And in 12.5 Free store :
I'm not sure your second function can ever be called as it is not a 'usual deallocation function'.