我可以对重载运算符 new 的类使用全局运算符 new 吗?

发布于 2024-09-18 00:08:30 字数 321 浏览 3 评论 0原文

假设我有一个带有重载operator new的类。

class Class {
public:
   void* operator new( size_t );
   void operator delete( void* );
};

当我使用new Class()时,该类的对象是否总是使用重载的operator new进行分配,或者是否可能使用默认的operator newnew 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 技术交流群。

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

发布评论

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

评论(3

情定在深秋 2024-09-25 00:08:30

使用作用域解析运算符 (::) 调用 operator new 的全局定义,否则它将始终使用重载的 operator new,当然 的情况除外放置新运算符 无论如何都不打算重载。

class Test {
public:
   void* operator new( size_t );
   void operator delete( void* );
};

int main()
{
Test* pTest1 = ::new Test();
Test* pTest2 = new Test();

/*
 do some stuff
*/

::delete pTest1;
delete pTest2;
return 0;
}

请注意删除运算符相应定义的使用。

Use scope-resolution operator (::) to call the global definition for operator new, otherwise it will always use the overloaded operator new, except ofcourse in case of placement new operator which is anyway not intended to be overloaded.

class Test {
public:
   void* operator new( size_t );
   void operator delete( void* );
};

int main()
{
Test* pTest1 = ::new Test();
Test* pTest2 = new Test();

/*
 do some stuff
*/

::delete pTest1;
delete pTest2;
return 0;
}

Note the use of corresponding definition for delete operator.

夏末 2024-09-25 00:08:30

除非您明确执行以下操作,否则将始终使用重载的operator newoperator delete

// Allocate using global new.
MyClass* my_object = ::new MyClass();

// Use object normally.
my_object->my_method();

// Deallocate using global delete.
::delete my_object;

或者,作为一个有点极端的说明性示例,如下所示:

#include <new>

// Use global placement new to allocate in a buffer created by global new.
MyClass* my_object = ::new(::operator new(sizeof(MyClass))) MyClass()

// Use object normally.
my_object->my_method();

// Explicitly invoke destructor.
my_object->~MyClass();

// Explicitly deallocate memory.
::operator delete(my_object);

希望这会有所帮助。

The overloaded operator new and operator delete will always be used unless you explicitly do something like this:

// Allocate using global new.
MyClass* my_object = ::new MyClass();

// Use object normally.
my_object->my_method();

// Deallocate using global delete.
::delete my_object;

Or, as a somewhat extreme illustrative example, something like this:

#include <new>

// Use global placement new to allocate in a buffer created by global new.
MyClass* my_object = ::new(::operator new(sizeof(MyClass))) MyClass()

// Use object normally.
my_object->my_method();

// Explicitly invoke destructor.
my_object->~MyClass();

// Explicitly deallocate memory.
::operator delete(my_object);

Hope this helps.

谈情不如逗狗 2024-09-25 00:08:30

我能想到的唯一可能的例外是placement new (此处讨论),但这是一种不同的语法。

The only possible exception I can think of is placement new (discussed here), but that's a different syntax.

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