Operator overloading outside class

This question already has an answer here: What are the basic rules and idioms for operator overloading? 7 answers The basic question is "Do you want conversions to be performed on the left-hand side parameter of an operator?" . If yes, use a free function. If no, use a class member. For example, for operator+() for strings, we want conversions to be performed so we can say thi

操作员在课外超载

这个问题在这里已经有了答案: 运算符重载的基本规则和习惯用法是什么? 7个答案 基本问题是“您是否希望在操作员的左侧参数上执行转换?” 。 如果是,请使用免费功能。 如果不是,请使用班级成员。 例如,对于字符串operator+() ,我们希望执行转换,以便我们可以这样说: string a = "bar"; string b = "foo" + a; 执行转换以将char * "foo"转换为std::string 。 所以,我们让字符串的operator+()变成一

What does it mean to have an undefined reference to a static member?

I just wrote a class with some static data members, but now I am getting errors about "undefined references". Why doesn't this work? What am I doing wrong? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to

对静态成员有一个未定义的引用意味着什么?

我刚刚用一些静态数据成员编写了一个类,但现在我得到关于“未定义引用”的错误。 为什么这不起作用? 我究竟做错了什么? (注意:这是一个Stack Overflow的C ++常见问题解答的入口,如果你想批评在这个表单中提供常见问题的想法,那么开始所有这些的meta上的贴子将成为这样做的地方。那个问题在C ++聊天室中进行监控,常见问题的想法首先出现在C ++聊天室中,所以你的答案很可能会被那些提出这个想法的人阅读。) 要理解这

Object destruction in C++

When exactly are objects destroyed in C++, and what does that mean? Do I have to destroy them manually, since there is no Garbage Collector? How do exceptions come into play? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answer

在C ++中的对象破坏

什么时候C ++中销毁的对象是什么,这是什么意思? 我是否必须手动销毁它们,因为没有垃圾收集器? 例外如何发挥作用? (注意:这是一个Stack Overflow的C ++常见问题解答的入口,如果你想批评在这个表单中提供常见问题的想法,那么开始所有这些的meta上的贴子将成为这样做的地方。那个问题在C ++聊天室中进行监控,常见问题的想法首先出现在C ++聊天室中,所以你的答案很可能会被那些提出这个想法的人阅读。) 在下面的文

c++

This is a follow-up to Critique my heap debugger from yesterday. As suggested by bitc, I now keep metadata about the allocated blocks in a separate handwritten hashtable. The heap debugger now detects the following kinds of errors: memory leaks (now with more verbose debugging output) illegal pointers passed to delete (that also takes care of double deletes) wrong form of delete (array v

C ++

这是昨天批评我的堆调试器的后续行动。 正如bitc所建议的,我现在将关于分配块的元数据保存在单独的手写散列表中。 堆调试器现在检测到以下类型的错误: 内存泄漏(现在有更详细的调试输出) 传递给删除的非法指针(也用于双重删除) 错误的删除形式(数组与非数组) 缓冲区溢出 缓冲区下溢 欢迎提前讨论并提前致谢! #include <cstdio> #include <cstdlib> #include <cstring> #include <n

How should I write ISO C++ Standard conformant custom new and delete operators?

How should I write ISO C++ standard conformant custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ, Operator overloading, and its follow-up, Why should one replace default new and delete operators? Section 1: Writing a standard-conformant new operator Part 1: Understanding the requirements for writing a custom new oper

我应该如何编写符合ISO C ++标准的自定义新的和删除操作符?

我应该如何编写符合ISO C ++标准的自定义new和delete操作符? 这是继续重载新的和删除的巨大照亮的C ++常见问题,运算符重载,及其后续,为什么要替换默认的新的和删除操作符? 第1部分:编写一个符合标准的new操作符 第1部分:了解编写自定义new操作符的要求 第2部分:了解new_handler要求 第3部分:了解特定场景要求 第2部分:编写符合标准的delete操作符 实现自定义删除操作符 (注意:这是一个Stack Overflo

What are the basic rules and idioms for operator overloading?

Note: The answers were given in a specific order, but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make most sense: The General Syntax of operator overloading in C++ The Three Basic Rules of Operator Overloading in C++ The Decision between Member and Non-member Common operators to over

运算符重载的基本规则和习惯用法是什么?

注意:答案是按照特定的顺序给出的,但是由于许多用户根据投票分类答案,而不是按照给定的时间排列答案,因此下面是答案的索引,按其最有意义的顺序排列: C ++中运算符重载的一般语法 C ++中运算符重载的三个基本规则 会员与非会员之间的决定 常见的操作符超载 作业操作员 输入和输出操作符 函数调用操作符 比较运算符 算术运算符 阵列下标 指针类型的操作符 转换运算符 重载新的和删除 (注意:这

Why would one replace default new and delete operators?

Why should would one replace the default operator new and delete with a custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ: Operator overloading. An followup entry to this FAQ is: How should I write ISO C++ standard conformant custom new and delete operators? Note: The answer is based on lessons from Scott Meyers&

为什么要替换默认的新运算符和删除运算符?

为什么人会替换默认的运营商new和delete一个自定义new和delete运营商? 这是在极其明亮的C ++ FAQ中重载新的和删除的继续: 运算符超载。 此常见问题解答的后续条目是: 我应该如何编写符合ISO C ++标准的自定义new和delete操作符? 注意:答案是基于Scott Meyers的More Effective C ++的经验教训。 (注意:这是一个Stack Overflow的C ++常见问题解答的入口,如果你想批评在这个表单中提供常见问题的想法,那么开始

What is an example of a difference in allowed usage or behavior between an xvalue and a prvalue FOR NON

What are rvalues, lvalues, xvalues, glvalues, and prvalues? gives a good overview of the taxonomy of rvalues/lvalues, and one of the recent answers to that question (https://stackoverflow.com/a/9552880/368896) stresses the point that prvalues are "like" the old-style rvalues, whereas the new xvalues allow for "lvalue-like" behavior. However, consider the following code: cl

什么是x值和prevalue FOR NON之间的允许使用或行为差异的例子

什么是右值,左值,xvalues,glvalues和prvalues? 给出了rvalues / lvalues分类的一个很好的概述,最近对这个问题的答案之一(https://stackoverflow.com/a/9552880/368896)强调了prvalues“像”旧式rvalues ,而新的xvalues允许“左值”行为。 但是,请考虑以下代码: class X {}; X foo() { return X(); } int main() { foo() = X(); // foo() is a prvalue that successfully appears on the lhs } 在这个例子中,表

How do I use arrays in C++?

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone ( std::vector<T> since C++98 and std::array<T, n> since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read legacy code or interact with a library written in C, you should have a firm grasp on how

我如何在C ++中使用数组?

C ++继承了C语言中的数组,几乎在任何地方都使用它们。 C ++提供了易于使用且易于出错的抽象(从C ++ 98开始,自C ++ 98和std::array<T, n>之后的std::vector<T> ),所以对数组的需求不会的出现频率与C中的频率相同。但是,当您阅读遗留代码或与用C语言编写的库进行交互时,应该牢牢掌握阵列的工作方式。 本常见问题分为五个部分: 类型级别的数组和访问元素 数组的创建和初始化 分配和参数传递 多维数

public friend swap member function

In the beautiful answer to the copy-and-swap-idiom there is a piece of code I need a bit of help: class dumb_array { public: // ... friend void swap(dumb_array& first, dumb_array& second) // nothrow { using std::swap; swap(first.mSize, second.mSize); swap(first.mArray, second.mArray); } // ... }; and he adds a note There are other claims t

公众朋友交换成员函数

在copy-and-swap-idiom的美丽答案中,我需要一些帮助: class dumb_array { public: // ... friend void swap(dumb_array& first, dumb_array& second) // nothrow { using std::swap; swap(first.mSize, second.mSize); swap(first.mArray, second.mArray); } // ... }; 并添加了一个便条 还有一些其他的声明,我们应该专门为我们的类型使用std :: swap,提供一个在