How could I sensibly overload placement operator new?(我怎么能合理地重载放置运算符 new?)
问题描述
C++ 允许重载 operator new - 全局和每个类 - 通常 operator new、operator new[] 与 一起使用new[] 语句和放置 operator new 分开.
C++ allows overloading operator new - both global and per-class - usual operator new, operator new[] used with new[] statement and placement operator new separately.
这三个中的前两个通常会因使用自定义分配器和添加跟踪而过载.但是放置 operator new 看起来很简单——它实际上什么都不做.例如,在 Visual C++ 中,默认实现只返回传递给调用的地址:
The former two of those three are usually overloaded for using customized allocators and adding tracing. But placement operator new seems pretty straightforward - it actually does nothing inside. For example, in Visual C++ the default implementation just returns the address passed into the call:
//from new.h
inline void* operator new( size_t, void* where )
{
return where;
}
它还能做什么?为什么以及如何合理地重载放置 operator new?
What else could it do? Why and how could I sensibly overload placement operator new?
推荐答案
正确答案是你不能替换新的运算符位置.
§18.4. 1.3 安置表格
这些函数是保留的,C++ 程序不能定义替换标准 C++ 库中版本的函数.
§18.4.1.3 Placement forms
These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library.
基本原理:分配和释放操作符的唯一目的是分配和释放内存,所以当给定内存时,什么都不用做.(标准特别指出这些功能故意不执行其他操作.")
The rationale: The only purpose of the allocation and deallocation operators is to allocate and deallocate memory, so when given memory nothing more should be done. (The standard specifically notes that these functions "Intentionally perform no other action.")
这篇关于我怎么能合理地重载放置运算符 new?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我怎么能合理地重载放置运算符 new?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
