什么是“:: operator new”和“:: operator delete”? [英] What are "::operator new" and "::operator delete"?

查看:228
本文介绍了什么是“:: operator new”和“:: operator delete”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 delete 是关键字。

int obj = new int;
delete obj;

int* arr = new int[1024];
delete[] arr;

< new> 的C ++标准头文件。它有两个运算符(我不知道他们是运算符还是函数):

<new> header is a part of C++ standard headers. It has two operators (I am not sure they are operators or they are functions):

:: operator new

:: operator delete

如下所示:

#include <new>
using namespace std;

int* buff = (int*)::operator new(1024 * sizeof(int));
::operator delete(buff);

什么是:: operator new和:: operator delete?它们与 new delete 关键字不同吗?

What are "::operator new" and "::operator delete"? Are they different from new and delete keywords?

推荐答案

:: 告诉编译器调用在全局命名空间中定义的运算符。

它是完全限定名对于全局删除运算符。

:: tells the compiler to call the operators defined in global namespace.
It is the fully qualified name for the global new and delete operators.

注您可以替换全局 delete 运算符以及类特定的 new delete 运算符。因此,在程序中可以有两个版本的 new delete 操作符。使用范围解析操作符的完全限定名称告诉编译器,您指的是运算符的全局版本,而不是类特定的。

Note that one can replace the global new and delete operators as well as overload class-specific new and delete operators. So there can be two versions of new and delete operators in an program. The fully qualified name with the scope resolution operator tells the compiler you are referring to the global version of the operators and not the class-specific ones.

这篇关于什么是“:: operator new”和“:: operator delete”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆