目录

运营商删除(operator delete)

描述 (Description)

它释放存储空间。

声明 (Declaration)

以下是operator 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)

参数 (Parameters)

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

  • nothrow_value - 它包含常量nothrow_value

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

  • voidptr2 - 它是一个void指针。

返回值 (Return Value)

没有

异常 (Exceptions)

No-throw guarantee - 此函数永远不会抛出异常。

数据竞争 (Data races)

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

例子 (Example)

在下面的例子解释了新的运算符。

#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
↑回到顶部↑
WIKI教程 @2018