我可以对重载运算符 new 的类使用全局运算符 new 吗?
假设我有一个带有重载operator new
的类。
class Class {
public:
void* operator new( size_t );
void operator delete( void* );
};
当我使用new Class()
时,该类的对象是否总是使用重载的operator new
进行分配,或者是否可能使用默认的operator new
当 new Class()
构造出现在代码中时使用?
Suppose I have a class with overloaded operator new
.
class Class {
public:
void* operator new( size_t );
void operator delete( void* );
};
Will objects of that class always be allocated with the overloaded operator new
when I use new Class()
or is it possible that the default operator new
is used when new Class()
construct appears in code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用作用域解析运算符 (::) 调用
operator new
的全局定义,否则它将始终使用重载的operator new
,当然的情况除外放置新运算符
无论如何都不打算重载。请注意删除运算符相应定义的使用。
Use scope-resolution operator (::) to call the global definition for
operator new
, otherwise it will always use the overloadedoperator new
, except ofcourse in case ofplacement new operator
which is anyway not intended to be overloaded.Note the use of corresponding definition for delete operator.
除非您明确执行以下操作,否则将始终使用重载的
operator new
和operator delete
:或者,作为一个有点极端的说明性示例,如下所示:
希望这会有所帮助。
The overloaded
operator new
andoperator delete
will always be used unless you explicitly do something like this:Or, as a somewhat extreme illustrative example, something like this:
Hope this helps.
我能想到的唯一可能的例外是placement new (此处讨论),但这是一种不同的语法。
The only possible exception I can think of is placement new (discussed here), but that's a different syntax.