C++把操作符也看成是与函数同样性质的实体,因此可以对操作符进行函数哪样的定义。例如:
#include<iostream>
using namespace std;
class Point {
int x, y;
public:
void set(int a, int b) {
x = a;
y = b;
}
void print() const {
cout << "(" << x << ", " << y << ")\n";
}
//friend声明后函数体中可以访问Point的私有数据
friend Point operator + (const Point &a, const Point &b); //“+”在匹配了某种类型的实参后便可以被编译理解成确定的行为
friend Point add(const Point &a, const Point &b); //add函数与+操作符的功能完全一致
};
Point operator + (const Point &a, const Point &b) {
Point s;
s.set(a.x + b.x, a.y + b.y);
return s;
}
Point add (const Point &a, const Point &b) {
Point s;
s.set(a.x + b.x, a.y + b.y);
return s;
}
int main() {
Point a, b;
a.set(3,2);
b.set(1,5);
(a + b).print(); //编译时转译为对operator + (a, b)的调用
operator + (a, b).print();
add(a, b).print();
}
Some characters or restraints about operator:
①不能创造新的操作符;
②::/./.*/?:这些操作符不能重载;
③操作符优先级不变;
④操作数个数不变;
⑤在操作符定义的参数表中,至少有一个参数必须是自定义类型;
⑥应与操作意义相符;
Increment operators
int a = 1, b = 1, c = 1, d = 1;
(++a) ++; //a = 3,函数定义为 X& operator ++ (X &a);
(b++) ++; //b = 2,函数定义为 X operator ++ (X &a, int b);其中参数int b是为区分前增量函数所设置的
分享到:
相关推荐
Teradata SQL Functions, Operators, Expressions, and Predicates
内容涉及:cdo(climate data operators)的说明书、指令卡片、图形使用文档、py和rb对cdo的使用以及一个大佬的相关ppt。之前发过一个链接,只有py和rb对cdo的使用,这边这个涵盖了我最近找到的所有参考文档。
Schrodinger Operators With Application to Quantum Mechanics and Global Geometry (Theoretical and Mathematical Physics) by Hans L. Cycon, Richard G. Froese, Werner Kirsch, Barry Simon (z-lib.org).djvu
微分方法求解Retinex算法,A unifying retinex model based on non-local differential operators
I get asked this question sometimes from seasoned programmers who are new to C++. There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet ...
Dunford,Schwartz写的线性算子。
This book should be useful for readers who are interested in the question Why and how something happened. Furthermore, it may serve as a reference and guide for beginners who want to learn Banach ...
v 若圆的半径为3cm,求圆的面积 n π x 32 n 3.14 * 3 * 3 v 若三名学生的身高分别为: 1.65, 1.78, 1.82,求他们的平均 身高 n n (1.65 + 1.78 + 1.82) / 3 1.65+1.78+1.82 3 算术运算符(Arithmetic Operators) ...
Evolutionary computation
介绍.NET Standard Query Operators
Restriction Operators Projection Operators Partitioning Operators Join Operators Concatenation Operators Ordering Operators Grouping Operators Set Operators Conversion Operators Equality ...
小波与算子 ,是小波领域权威书籍,适合研究小波的学者。
适合入门的泛函分析类书籍,由浅入深,有大量应用和例子,
Lecture Notes in Mathematics Edited by A. Dold and B. Eckmann Applications to Partial Differential Equations
JSON Functions And Operators.md
Calder'on-Zygmund 算子的交换子的加权端点估计,梁熠宇, KY LUONG DANG,设 $deltain(0,1]$, $T$ 是一个 $delta$-Calder'on-Zygmund 算子.设 $w$ 属于 Muckenhoupt 权类 $A_{1+delta/n}({mathbb R}^n)$, 满足$int_...
《如何编写Operator》Operator 模式旨在捕获(正在管理一个或一组服务的)运维人员的关键目标。 负责特定应用和 service 的运维人员,在系统应该如何运行、如何部署以及出现问题时如何处理等方面有深入的了解。...
r6运算符 r6operators是Node.js的高质量矢量化Rainbow 6:Siege Operator图标和元数据的集合。 这个项目的开始是人们获得“彩虹六号:围攻”操作员的高分辨率操作员图标的一种方式,尤其是随着近年来矢量图形在Web...
standard_query_operatorsstandard_query_operators
安装yarn add -D eslint-plugin-no-mixed-operators 要么npm install --save-dev eslint-plugin-no-mixed-operators用法像常规插件一样使用它,并禁用原始插件(如果已启用)。 // .eslintrc{ "plugins" : [ "no-...