对重载new和delete施加限制
是否可以对重载运算符 new 和 delete 施加一些限制? 我的重载 new 在另一个文件中链接到我的测试程序。
场景是:
if(condition is satisfied)
call overloaded new
else
call the actual new defined in new.h
Is it possible to put some restrictions in overloading operators new and delete?
My overloaded new is linked in a different file to my test program.
The scenario is:
if(condition is satisfied)
call overloaded new
else
call the actual new defined in new.h
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
始终使用重载的
new
/delete
并检查其实现中的条件。Always use your overloaded
new
/delete
and check your condition inside its implementation.一旦你替换了默认的
::operator new()
你就不能再使用它了——它就永远消失了。 查看此问题。如果您想获得原始
::operator new()
的效果,您必须重新实现它,这并不难。Once you replace the default
::operator new()
you can't use it anymore - it's gone forever. See this question.If you want to have the effect of the original
::operator new()
you'll have to reimplement it which isn't very hard.可以通过三种方式为运营商提供新的服务。
替换四个非放置默认运算符 new 中的一个或多个,
为默认运算符 new 提供重载(因此带有一个附加参数,这些可以使用placement new语法调用),
提供operator new类成员,那些将仅为该类及其后代调用。
在后两种情况下,可以使用以下语法调用更知名的运算符 new 之一:
但如果您已替换它们,则将调用您的替换项。 您不能将已替换的默认运算符称为新的(也许您可以通过使用特定于实现的链接器技巧来实现,但这超出了语言的范围)。
关于operator new的替换:
There are three ways to provide an operator new.
replacing one or more of the four non placement default operators new,
providing overload to the default operator new (thus with an additional parameter, those may be called with the placement new syntax),
providing operator new class members, those will be called only for that class and their descendant.
In the latter two cases, it is possible to call one of the more well know operators new with the syntax:
but if you have replaced them, your replacement will be called. You can't call the default operators new you have replaced (well perhaps you can by playing implementation specific linker tricks, but that's outside the scope of the language).
About the replacement of operator new:
您可以轻松地在重载的 new 运算符中执行检查。确保实现 new 运算符的所有风格(正如 AProgrammer 已经指出的那样)。
调用原来的/默认的new是不可能的,但是自己实现也不难。毕竟new只是分配内存而已。因此,您也可以调用 malloc、HeapAlloc 或系统上找到的任何内存分配例程,而不是调用原始/默认的 new。请务必在删除的实现中调用相应的内存释放方法(free、HeapFree,...)。
你没有告诉你要在 new 的实现中检查什么样的条件?如果它是“静态”条件(我的意思是:在应用程序执行期间始终给出相同的结果),则还应该将相同的条件添加到删除的实现中。
如果条件取决于情况并在运行应用程序时发生变化,您应该预见一种方法,您可以通过该方法知道在删除函数中使用哪个删除实现。对此的一个技巧如下:
在 new 的实现中:
在您的删除实现中:
You can easily perform the check in your overloaded new operator. Be sure to implement all flavours of the new operator (as AProgrammer already pointed out).
Calling the original/default new is not possible, but it's not difficult to implement it yourself. After all, new only allocates memory, that's it. So instead of calling the original/default new, you can also call malloc, HeapAlloc, or any memory-allocation routine found on your system. Be sure to call the corresponding memory-deallocation method (free, HeapFree, ...) in your implementation of delete.
You didn't tell what kind of condition you are going to check in your implementation of new? If it's a 'static' condition (I mean: always giving the same result during the execution of your application), the same condition should also be added to your implementation of delete.
If the condition depends on the situation and changes while running your application, you should foresee a method where you can know which delete implementation to use in your delete function. One trick do to this is the following:
In your implementation of new:
In your implementation of delete: