C++ 新库 - 运算符删除


描述

它释放存储空间。

宣言

以下是运算符 new[] 的声明。

	
void operator delete (void* ptr) throw();	(ordinary delete)
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw();	    (nothrow delete)
void operator delete (void* ptr, void* voidptr2) throw();             (placement delete)

C++11

		
void operator delete (void* ptr) noexcept;   (ordinary delete)
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) noexcept;	    (nothrow delete)
void operator delete (void* ptr, void* voidptr2) noexcept;      (placement delete)

C++14

		
void operator delete (void* ptr) noexcept;                    (ordinary delete)
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) noexcept;             (nothrow delete)
void operator delete (void* ptr, void* voidptr2) noexcept;                (placement delete)
void operator delete (void* ptr, std::size_t size) noexcept;	            (ordinary delete with size)
void operator delete (void* ptr, std::size_t size,
                      const std::nothrow_t& nothrow_constant) noexcept;            (ordinary delete with size)

参数

  • size - 它包含请求的内存块的大小(以字节为单位)。

  • nothrow_value - 它包含常量 nothro。

  • ptr - 它是指向已分配的适当大小的内存块的指针。

  • voidptr2 - 它是一个空指针。

返回值

没有任何

例外情况

无抛出保证- 该函数永远不会抛出异常。

数据竞赛

它修改返回值引用的存储。

例子

在下面的示例中解释了 new 运算符。

#include <iostream>

struct MyClass {
   MyClass() {std::cout <<"It is a MyClass() constructed\n";}
   ~MyClass() {std::cout <<"It is a MyClass() destroyed\n";}
};

int main () {
   MyClass * pt = new (std::nothrow) MyClass;
   delete pt;

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

It is a MyClass() constructed
It is a MyClass() destroyed
新的.htm