C++里的操作符重载:理解operator的关键字
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名。
这是C+ +扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算符的使用方法与其原来一致,另一方面扩展其功能只能通过函数的方式(c++中,“功能”都是由函数实现的)。
一、为什么使用操作符重载?
对于系统的所有操作符,一般情况下,只支持基本数据类型和标准库中提供的class,对于用户自己定义的class,如果想支持基本操作,比如比较大小,判断是否相等,等等,则需要用户自己来定义关于这个操作符的具体实现。
比如,判断两个人是否一样大,我们默认的规则是按照其年龄来比较,所以,在设计person 这个class的时候,我们需要考虑操作符==,而且,根据刚才的分析,比较的依据应该是age。那么为什么叫重载呢?这是因为,在编译器实现的时候,已经为我们提供了这个操作符的基本数据类型实现版本,但是现在他的操作数变成了用户定义的数据类型class,所以,需要用户自己来提供该参数版本的实现。
二、如何声明一个重载的操作符?
A: 操作符重载实现为类成员函数
重载的操作符在类体中被声明,声明方式如同普通成员函数一样,只不过他的名字包含关键字operator,以及紧跟其后的一个c++预定义的操作符。
可以用如下的方式来声明一个预定义的==操作符:
class person{
private:
int age;
public:
person(int a){
this->age=a;
}
inline bool operator == (const person &ps) const;
};
// 实现方式
inline bool person::operator==(const person &ps) const
{
if (this->age==ps.age)
return true;
return false;
}
// 调用方式
#include
using namespace std;
int main()
{
person p1(10);
person p2(20);
if(p1==p2){
cout<<”the age is equal!”<<endl;
}
return 0;
}
这里,因为operator == 是class person的一个成员函数,所以对象p1,p2都可以调用该函数,上面的if语句中,相当于p1调用函数 ==,把p2作为该函数的一个参数传递给该函数,从而实现了两个对象的比较。
B:操作符重载实现为非类成员函数(全局函数)
对于全局重载操作符,代表左操作数的参数必须被显式指定。例如:
#include
#include
using namespace std;
class person
{
public:
int age;
public:
};
bool operator==(person const &p1 ,person const & p2)
//满足要求,做操作数的类型被显示指定
{
if(p1.age==p2.age)
return true;
return false;
}
int main()
{
person rose;
person jack;
rose.age=18;
jack.age=23;
if(rose==jack){
cout<<"ok"<<endl;
}
return 0;
}
C:如何决定把一个操作符重载为类成员函数还是全局名字空间的成员呢?
①如果一个重载操作符是类成员,那么只有当与他一起使用的左操作数是该类的对象时,该操作符才会被调用。如果该操作符的左操作数必须是其他的类型,则操作符必须被重载为全局名字空间的成员。
②C++要求赋值=,下标[],调用(), 和成员指向-> 操作符必须被定义为类成员操作符。任何把这些操作符定义为名字空间成员的定义都会被标记为编译时刻错误。
③如果有一个操作数是类类型如string类的情形那么对于对称操作符比如等于操作符最好定义为全局名字空间成员。
D:重载操作符具有以下限制:
(1) 只有C++预定义的操作符集中的操作符才可以被重载;
(2)对于内置类型的操作符,它的预定义不能被改变,应不能为内置类型重载操作符,如,不能改变int型的操作符+的含义;
(3) 也不能为内置的数据类型定义其它的操作符;
(4) 只能重载类类型或枚举类型的操作符;
(5) 重载操作符不能改变它们的操作符优先级;
(6) 重载操作符不能改变操作数的个数;
(7) 除了对( )操作符外,对其他重载操作符提供缺省实参都是非法的;
E: 注意点:
(1)后果载操操作符首先要确定它的返回值是左值,还是右值,如果是左值最返回引用,如果是右值那就直接返回值;
(2) +号等这样的操作符没有对象可以容纳改变后值,对于这样的情况最好返回数值,否则只能要操作符体内创建临时对象用于容纳改变后的值,如果在堆中创建临时对象返回指针或者引用,在操作符函数体外还需要释放它,如果返回的对象而不是引用或者指针,那么效率是比较低的。如果返回的是数值,最好在该类的构造函数中增加对该类型数值的转换函数,如:返回值是int类型,那么最好有一个int类型作为参数的构造函数。
(3)在增量运算符中,放上一个整数形参,就是后增量运行符,它是值返回,对于前增量没有形参,而且是引用返回,示例:
class Test
{
public:
Test(x=3){ m_value = x}
Test &operator ++(); //前增量
Test &operator ++(int);//后增量
private:
Int m_value:
};
Test &Test::operator ++()
{
m_value ++; //先增量
return *this; //返回当前对象
}
Test Test::operator ++(int)
{
Test tmp(*this); //创建临时对象
m_value ++; //再增量
return temp; //返回临时对象
}
(4)因为强制转换是针对基本数据类型的,所以对类类型的转换需自定义;
(5) 转换运行符重载声明形式:operator 类型名();它没有返回类型,因为类型名就代表了它的返回类型,所以返回类型显得多余。
(6)一般来说,转换运算符与转换构造函数(即带一个参数的构造函数)是互逆的,如有了构造函数Test(int),那么最好有一个转换运算符int()。这样就不必提供对象参数重载运算符了,如Test a1(1);Test a2(2); Test a3; a3 = a1+a2;就不需要重载+号操作符了,因为对于a1+a2的运算,系统可能会先找有没有定义针对Test的+号操作符,如果没有,它就会找有没有针对Test类转换函数参数类型的+号操作符(因为可以将+号运行结果的类型通过转换函数转换为Test对象),因为Test类有个int类型的参数,对于int类型有+操作符,所以a1+a2真正执行的是Test(int(a1) + int(a2));即Test(3);
(7)对于转换运算符,还有一个需要注意的地方就是,如果A类中有以B为参数的转换函数(构造函数),那B中不能有A的转换运算符,不然就存在转换的二义性,如:
class A{A(B&){…}}; class B{ operator A(){…}};
那么以下语句就会有问题:
B b; A(b);//A(b)有就可能是A的构造函数,也可以是B的转换运算符
重载的意义
在面向对象编程时,常常会建立一个类,例如建立一个矩形类,想判断其中两个对象(我声明的两个矩形)相等,则必须有长相等、宽相等;如果要写一个函数来进行比较,会不如我们常用的“==”运算符直观简单:
class rectangle{
private:
int length, width;
public:
rectangle(int l, int w){
length = l;
width = w;
}
bool IsSame(const rectangle&); //比较函数
bool operator==(const rectangle&); //重载"=="运算符
};
bool rectangle::IsSame(const rectangle& a){
if(length==a.length&&width==a.width){
return true;
}
else return false;
}
bool rectangle::operator==(const rectangle& a){
if(length==a.length&&width==a.width){
return true;
}
else return false;
}
int main(){
rectangle A(5,5);
rectangle B(5,5);
if(A.IsSame(B)){
cout<<"Same"<<endl;
}
if(A==B){ //符合语言习惯 更为直观
cout<<"Same~"<<endl;
}
return 0;
}
所以,这个使得“==”运算符能被用户定义的类使用的过程就是“重载”。
重载的要求
1.不能改变运算符的初始意义。
2.不能改变运算符的参数数目。如重载运算符+时只用一个操作数是错误的。
3.运算符函数不能包括缺省的参数。
4.绝大部分C++运算符都可以重载,以下的例外: . :: .* ?
5.除赋值运算符外,其它运算符函数都可以由派生类继承。
6.运算符重载不改变运算符的优先级和结合性,也不改变运算符的语法结构,即单目、双目运算符只能重载为单目、双目运算符。
7.运算符的重载实际上是函数的重载。编译程序对运算符重载的选择,遵循函数重载的选择原则。当遇到不很明显的运算符时,编译程序将去寻找参数匹配的运算符函数。
8.运算符重载可使程序更简洁,使表达式更直观,增强可读性。但使用不宜过多。
9.重载运算符含义必须清楚
重载的形式
可以将操作符重载为 成员函数形式 和 友元函数形式。
(1) 一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。
(2) 以下双目运算符不能重载为类的友元函数:=、()、[]、->。
(3) 类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。
(4) 若一个运算符的操作需要修改对象的状态,选择重载为成员函数较好。
(5) 若运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则只能选用友元函数。
(6) 当运算符函数是一个成员函数时,最左边的操作数(或者只有最左边的操作数)必须是运算符类的一 个类对象(或者是对该类对象的引用)。如果左边的操作数必须是一个不同类的对象,或者是一个内部 类型的对象,该运算符函数必须作为一个友元函数来实现。
(7) 当需要重载运算符具有可交换性时,选择重载为友元函数。
对此,我个人有一种自己的理解:
考虑操作符重载为哪种形式时,可以从该操作符的“使用者”层面上来思考,
比如常见的“=”、“+=”、“=”、“--”、“++”等,使用者都是“对象”,由“对象”来“使用”,所以定义为类的成员函数。(如上“==”的重载)
其他操作符如“+”、“-”、“”、“/”、“%”的“使用者”应该是其两边的内容,所以定义为友元函数,赋予其访问私有成员的权利即可。
举个栗子 Int 是一个模拟整形的类:
bool operator ==(const Int&a) {
if (i == a.i) return true;
else return false;
}
void operator +=(const Int&a) {
i += a.i;
}
/*类成员*/
/*友元函数*/
friend double operator*(const double a, const Int& e) {
double c;
c = a * e.i;
return c;
}
friend double operator*(const Int& e, const double a) {
double c;
c = e.i * a;
return c;
}
friend int operator*(const int a, const Int& e) {
int c;
c = a * e.i;
return c;
}
friend int operator*(const Int& e, const int a) {
int c;
c = e.i*a;
return c;
}
friend int operator*(const Int& e, const Int& a) {
int c;
c = e.i*a.i;
return c;
}
输入输出流重载
继续以此段代码为例,重载输入输出 ">>" "<<" ,详见代码:
#include<iostream>
using namespace std;
class rectangle{
private:
int length, width;
public:
rectangle(int l, int w){
length = l;
width = w;
}
friend istream &operator>>(istream &in, rectangle &a);//重载输入流
friend ostream &operator<<(ostream &os, rectangle &a);//重载输出流
};
istream &operator>>(istream &in, rectangle &a){
in >> a.length >> a.width;
return in;
}
ostream &operator<<(ostream &os, rectangle &a){
os << a.length << endl << a.width << endl;
return os;
}
int main(){
rectangle A(5,5);
rectangle B(5,5);
cin >> A;
cout << A;
cout << B;
return 0;
}
类型转换运算符重载函数
这一点对于operator关键字的运用,除非查询时就输入这“生僻”的名称:“类型转换运算符重载函数“ 或者 ”类型转换函数“,否则并不容易查找到相关的资料…
详见 http://en.cppreference.com/w/cpp/language/cast_operator
简单地说,即是在类的内部声明
operator 类型名( )
{
实现转换的语句
}
如代码所示:
#include<iostream>
using namespace std;
class rectangle{
private:
int length, width;
public:
rectangle(int l, int w){
length = l;
width = w;
}
operator int() const{
return length*width;
}
};
istream &operator>>(istream &in, rectangle &a){
in >> a.length >> a.width;
return in;
}
ostream &operator<<(ostream &os, rectangle &a){
os << a.length << " " << a.width << endl;
return os;
}
int main(){
rectangle A(5,5);
rectangle B(5,5);
int area = A;
cout << area << endl;
int area2;
area2 = area + B;
cout << area2 << endl;
return 0;
}
operator重载的例子:
#include <iostream>
using namespace std;
class A
{
public:
A(double _data = 0.0):data(_data){}
A& operator = (const A& rhs)
{
data = rhs.data;
return *this;
}
friend A operator + (const A& lhs,const A& rhs);
friend A operator - (const A& lhs,const A& rhs);
friend A operator * (const A& lhs,const A& rhs);
friend A operator + (const A& lhs,double rhs);
friend A operator + (double lhs,const A& rhs);
friend A operator * (const A& lhs,double rhs);
friend A operator * (double lhs,const A& rhs);
friend A operator - (const A& lhs,double rhs);
friend A operator - (double lhs,const A& rhs);
friend ostream& operator << (ostream& fout,A& a);
// A& operator += (const A& rhs);
// A& operator -= (const A& rhs);
// A& operator *= (const A& rhs);
private:
double data;
};
A operator + (const A& lhs,const A& rhs)
{
A res(0);
res.data = lhs.data + rhs.data;
return res;
}
A operator - (const A& lhs,const A& rhs)
{
A res(0);
res.data = lhs.data - rhs.data;
return res;
}
A operator * (const A& lhs,const A& rhs)
{
A res(0);
res.data = lhs.data * rhs.data;
return res;
}
A operator + (const A& lhs,double rhs)
{
A res(0);
res.data = lhs.data + rhs;
return res;
}
A operator + (double lhs,const A& rhs)
{
A res(0);
res.data = lhs + rhs.data;
return res;
}
A operator * (const A& lhs,double rhs)
{
A res(0);
res.data = lhs.data * rhs;
return res;
}
A operator * (double lhs,const A& rhs)
{
A res(0);
res.data = lhs * rhs.data;
return res;
}
A operator - (const A& lhs,double rhs)
{
A res(0);
res.data = lhs.data - rhs;
return res;
}
A operator - (double lhs,const A& rhs)
{
A res(0);
res.data = lhs - rhs.data;
return res;
}
ostream& operator << (ostream& fout,A& a)
{
fout << a.data ;
return fout;
}
int main(int argc, char* argv[])
{
A a(2.3);
A b(1.2);
A d(3.4);
A c;
c = a + b + d;
c=a+b;
c=a+1.0;
c=a-b;
c=a-1.0;
c=a*b;
c=a*1.0;
cout << c << endl;
c=1.0+2.0*a*a-3.0*a*b;
cout << c << endl;
return 0;
}
输出结果:
更多参考 一 二 三 四
上一篇: 用Python实现基于图片的塔伯自我指涉K值生成程序
下一篇: 知乎大神分享的罗素问题解析
推荐阅读
-
C++ 的对象模型及指针、好友和操作符重载
-
深入理解C++中的函数重载:从C的基础出发
-
C++里的操作符重载:理解operator的关键字
-
F#探险之旅(二):函数式编程(上)-函数式编程范式简介 F#主要支持三种编程范式:函数式编程(Functional Programming,FP)、命令式编程(Imperative Programming)和面向对象(Object-Oriented,OO)的编程。回顾它们的历史,FP是最早的一种范式,第一种FP语言是IPL,产生于1955年,大约在Fortran一年之前。第二种FP语言是Lisp,产生于1958,早于Cobol一年。Fortan和Cobol都是命令式编程语言,它们在科学和商业领域的迅速成功使得命令式编程在30多年的时间里独领风骚。而产生于1970年代的面向对象编程则不断成熟,至今已是最流行的编程范式。有道是“*代有语言出,各领风骚数十年”。 尽管强大的FP语言(SML,Ocaml,Haskell及Clean等)和类FP语言(APL和Lisp是现实世界中最成功的两个)在1950年代就不断发展,FP仍停留在学院派的“象牙塔”里;而命令式编程和面向对象编程则分别凭着在商业领域和企业级应用的需要占据领先。今天,FP的潜力终被认识——它是用来解决更复杂的问题的(当然更简单的问题也不在话下)。 纯粹的FP将程序看作是接受参数并返回值的函数的集合,它不允许有副作用(side effect,即改变了状态),使用递归而不是循环进行迭代。FP中的函数很像数学中的函数,它们都不改变程序的状态。举个简单的例子,一旦将一个值赋给一个标识符,它就不会改变了,函数不改变参数的值,返回值是全新的值。 FP的数学基础使得它很是优雅,FP的程序看起来往往简洁、漂亮。但它无状态和递归的天性使得它在处理很多通用的编程任务时没有其它的编程范式来得方便。但对F#来说这不是问题,它的优势之一就是融合了多种编程范式,允许开发人员按照需要采用最好的范式。 关于FP的更多内容建议阅读一下这篇文章:Why Functional Programming Matters(中文版)。F#中的函数式编程 从现在开始,我将对F#中FP相关的主要语言结构逐一进行介绍。标识符(Identifier) 在F#中,我们通过标识符给值(value)取名字,这样就可以在后面的程序中引用它。通过关键字let定义标识符,如: let x = 42 这看起来像命令式编程语言中的赋值语句,两者有着关键的不同。在纯粹的FP中,一旦值赋给了标识符就不能改变了,这也是把它称为标识符而非变量(variable)的原因。另外,在某些条件下,我们可以重定义标识符;在F#的命令式编程范式下,在某些条件下标识符的值是可以修改的。 标识符也可用于引用函数,在F#中函数本质上也是值。也就是说,F#中没有真正的函数名和参数名的概念,它们都是标识符。定义函数的方式与定义值是类似的,只是会有额外的标识符表示参数: let add x y = x + y 这里共有三个标识符,add表示函数名,x和y表示它的参数。关键字和保留字关键字是指语言中一些标记,它们被编译器保留作特殊之用。在F#中,不能用作标识符或类型的名称(后面会讨论“定义类型”)。它们是: abstract and as asr assert begin class default delegate do donedowncast downto elif else end exception extern false finally forfun function if in inherit inline interface internal land lazy letlor lsr lxor match member mod module mutable namespace new nullof open or override private public rec return sig static structthen to true try type upcast use val void when while with yield 保留字是指当前还不是关键字,但被F#保留做将来之用。可以用它们来定义标识符或类型名称,但编译器会报告一个警告。如果你在意程序与未来版本编译器的兼容性,最好不要使用。它们是: atomic break checked component const constraint constructor continue eager event external fixed functor global include method mixinobject parallel process protected pure sealed trait virtual volatile 文字值(Literals) 文字值表示常数值,在构建计算代码块时很有用,F#提供了丰富的文字值集。与C#类似,这些文字值包括了常见的字符串、字符、布尔值、整型数、浮点数等,在此不再赘述,详细信息请查看F#手册。 与C#一样,F#中的字符串常量表示也有两种方式。一是常规字符串(regular string),其中可包含转义字符;二是逐字字符串(verbatim string),其中的(")被看作是常规的字符,而两个双引号作为双引号的转义表示。下面这个简单的例子演示了常见的文字常量表示: let message = "Hello World"r"n!" // 常规字符串let dir = @"C:"FS"FP" // 逐字字符串let bytes = "bytes"B // byte 数组let xA = 0xFFy // sbyte, 16进制表示let xB = 0o777un // unsigned native-sized integer,8进制表示let print x = printfn "%A" xlet main = print message; print dir; print bytes; print xA; print xB; main Printf函数通过F#的反射机制和.NET的ToString方法来解析“%A”模式,适用于任何类型的值,也可以通过F#中的print_any和print_to_string函数来完成类似的功能。值和函数(Values and Functions) 在F#中函数也是值,F#处理它们的语法也是类似的。 let n = 10let add a b = a + blet addFour = add 4let result = addFour n printfn "result = %i" result 可以看到定义值n和函数add的语法很类似,只不过add还有两个参数。对于add来说a + b的值自动作为其返回值,也就是说在F#中我们不需要显式地为函数定义返回值。对于函数addFour来说,它定义在add的基础上,它只向add传递了一个参数,这样对于不同的参数addFour将返回不同的值。考虑数学中的函数概念,F(x, y) = x + y,G(y) = F(4, y),实际上G(y) = 4 + y,G也是一个函数,它接收一个参数,这个地方是不是很类似?这种只向函数传递部分参数的特性称为函数的柯里化(curried function)。 当然对某些函数来说,传递部分参数是无意义的,此时需要强制提供所有参数,可是将参数括起来,将它们转换为元组(tuple)。下面的例子将不能编译通过: let sub(a, b) = a - blet subFour = sub 4 必须为sub提供两个参数,如sub(4, 5),这样就很像C#中的方法调用了。 对于这两种方式来说,前者具有更高的灵活性,一般可优先考虑。 如果函数的计算过程中需要定义一些中间值,我们应当将这些行进行缩进: let halfWay a b = let dif = b - a let mid = dif / 2 mid + a 需要注意的是,缩进时要用空格而不是Tab,如果你不想每次都按几次空格键,可以在VS中设置,将Tab字符自动转换为空格;虽然缩进的字符数没有限制,但一般建议用4个空格。而且此时一定要用在文件开头添加#light指令。作用域(Scope)作用域是编程语言中的一个重要的概念,它表示在何处可以访问(使用)一个标识符或类型。所有标识符,不管是函数还是值,其作用域都从其声明处开始,结束自其所处的代码块。对于一个处于最顶层的标识符而言,一旦为其赋值,它的值就不能修改或重定义了。标识符在定义之后才能使用,这意味着在定义过程中不能使用自身的值。 let defineMessage = let message = "Help me" print_endline message // error 对于在函数内部定义的标识符,一般而言,它们的作用域会到函数的结束处。 但可使用let关键字重定义它们,有时这会很有用,对于某些函数来说,计算过程涉及多个中间值,因为值是不可修改的,所以我们就需要定义多个标识符,这就要求我们去维护这些标识符的名称,其实是没必要的,这时可以使用重定义标识符。但这并不同于可以修改标识符的值。你甚至可以修改标识符的类型,但F#仍能确保类型安全。所谓类型安全,其基本意义是F#会避免对值的错误操作,比如我们不能像对待字符串那样对待整数。这个跟C#也是类似的。 let changeType = let x = 1 let x = "change me" let x = x + 1 print_string x 在本例的函数中,第一行和第二行都没问题,第三行就有问题了,在重定义x的时候,赋给它的值是x + 1,而x是字符串,与1相加在F#中是非法的。 另外,如果在嵌套函数中重定义标识符就更有趣了。 let printMessages = let message = "fun value" printfn "%s" message; let innerFun = let message = "inner fun value" printfn "%s" message innerFun printfn "%s" message printMessages 打印结果: fun value inner fun valuefun value 最后一次不是inner fun value,因为在innerFun仅仅将值重新绑定而不是赋值,其有效范围仅仅在innerFun内部。递归(Recursion)递归是编程中的一个极为重要的概念,它表示函数通过自身进行定义,亦即在定义处调用自身。在FP中常用于表达命令式编程的循环。很多人认为使用递归表示的算法要比循环更易理解。 使用rec关键字进行递归函数的定义。看下面的计算阶乘的函数: let rec factorial x = match x with | x when x < 0 -> failwith "value must be greater than or equal to 0" | 0 -> 1 | x -> x * factorial(x - 1) 这里使用了模式匹配(F#的一个很棒的特性),其C#版本为: public static long Factorial(int n) { if (n < 0) { throw new ArgumentOutOfRangeException("value must be greater than or equal to 0"); } if (n == 0) { return 1; } return n * Factorial (n - 1); } 递归在解决阶乘、Fibonacci数列这样的问题时尤为适合。但使用的时候要当心,可能会写出不能终止的递归。匿名函数(Anonymous Function) 定义函数的时候F#提供了第二种方式:使用关键字fun。有时我们没必要给函数起名,这种函数就是所谓的匿名函数,有时称为lambda函数,这也是C#3.0的一个新特性。比如有的函数仅仅作为一个参数传给另一个函数,通常就不需要起名。在后面的“列表”一节中你会看到这样的例子。除了fun,我们还可以使用function关键字定义匿名函数,它们的区别在于后者可以使用模式匹配(本文后面将做介绍)特性。看下面的例子: let x = (fun x y -> x + y) 1 2let x1 = (function x -> function y -> x + y) 1 2let x2 = (function (x, y) -> x + y) (1, 2) 我们可优先考虑fun,因为它更为紧凑,在F#类库中你能看到很多这样的例子。 注意:本文中的代码均在F# 1.9.4.17版本下编写,在F# CTP 1.9.6.0版本下可能不能通过编译。 F#系列随笔索引页面