- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能给我解释一下 C++ primer 第 5 版中的这段话:
Terminology: new Expression versus operator new Function
The library functions operator new and operator delete aremisleadingly named. Unlike other operator functions, such as operator=,these functions do not overload the new or delete expressions. In fact, wecannot redefine the behavior of the new and delete expressions.
A new expression always executes by calling an operator new functionto obtain memory and then constructing an object in that memory. A deleteexpression always executes by destroying an object and then calling anoperator delete function to free the memory used by the object.
By providing our own definitions of the operator new and operatordelete functions, we can change how memory is allocated. However, wecannot change this basic meaning of the new and delete operators.
我没有看到 operator new
或 operator delete
与任何其他重载运算符(如赋值运算符 =
)之间的区别。那么“被误导命名”是什么意思呢?我们都知道我们不会重写像 fObj + fObj
这样的表达式,但我们重载了运算符而不是表达式本身。
事实上,我发现这一段本身具有误导性。毕竟我们可以“滥用”任何可重载运算符以及从哪个运算符new
和delete
那么他在这一段中的意思是什么?谢谢!
最佳答案
C++ 中的大多数运算符不受任何显式语义或要求的约束。唯一真正的异常(exception)是 operator new
和 operator delete
,因为它的特殊用途,这就是它所指的。
例如,让我们考虑operator==
。
尽管让此运算符执行某种比较并返回 bool
以指示相等是常规(而且是明智的)——这实际上不是 C++ 语言所要求的。
事实上,实际上完全有可能定义 operator==
来返回一些完全不相关的东西——也许是一个 int
,一个 std::string
,或者像 std::tuple
这样更奇怪的东西。而且,当然,它不需要实际上执行任何比较。
实际上,C++ 中大多数运算符的语义是弱需求的约定,但不是语言。
这与 operator new
和 operator delete
形成对比。 C++ 中的 new
表达式将始终在 operator new
调用返回的指针处开始动态对象的生命周期。这是否是 new (p) T{...}
用于 placement-new 表达式,new T{...}
与 global-new
运算符,或一些 new(args,...) T{...}
用于自定义 operator new
-- 它必须返回某种形式T
开始生命周期的指针。
同样,delete
必须结束该生命周期,并调用operator delete
释放该生命周期的底层存储。这有效地迫使 operator new
和 operator delete
的语义分别执行某种形式的分配机制和某种形式的清理机制。实际上不可能定义 new
和 delete
来做一些奇怪的事情(如 operator==
的情况),因为隐含的行为调用这些运算符只会中断(如果它完全编译的话)。
这就是为什么引用中提到不能重新定义operator new
/operator delete
的行为;基本意义永远是固定的。
关于C++ 入门第 5 版 : operator new and operator delete overloading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67409792/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!