运算符重载注意点:

1.算术和关系操作符返回的是一个左值或右值,而不是一个引用

2.赋值操作符一定要定义为成员函数如“=”

3.一般而言,赋值操作符和复合赋值操作符应返回左操作数的引用如"="和''+="

C++不允许赋值运算符被重载为全局形式,这是因为如果可以写出全局形式的赋值运算符函数的话,我们可以写出这样的函数:

int operator=(int a, integer b);

从而出现这样的语句:

integer a(3); 2 = a;//God save me

现在我们写一个简单的integer类并重载赋值运算符:

因为在自赋值的情况下可能给对象造成伤害,所以在重载赋值运算符时必须要注意自赋值的情况

所以integer类中的赋值运算符函数应写成这样:

integer& integer::operator=(const integer& a) { if(this != &a) i = a.i; return *this; };

 

1、 运算符重载的概念

      运算符重载是C++的重要组成部分,它可以让程序更加简单易懂,简单的运算符可以使复杂函数的理解更直观。对于普通对象来说可以使用算术运算符让它们参与计算,C++也允许为类的对象构造运算符来实现单目或双目运输,这个特性就叫运算符的重载。其实,任何使用运算符完成的功能,使用普通的函数也能够完成。运算符的重载主要存在两种形式,一种是作为类的友元函数进行使用,另一种则是作为类的成员函数进行使用。运算符的重载的形式为:

2、 运算符的运算规则

返回类型 operator 运算符符号(参数说明)
{     //函数体的内部实现

}

javascript 运算符重载 运算符重载operator_赋值运算符


      ①运算符重载函数也是函数,重载的运算符不会改变运算符的优先级、结合型和参数的个数。

      ②重载运算符不能违反语言的语法规则。

      ③赋值运算符除外,重载运算符可由派生类继承下去。

      ④重载运算符不能使用默认参数。

      ⑤运算符=、()、[]和->可作为类成员运算符,不能作为友元运算符。

      ⑥运算符“.”、“::”、“?:”不能重载。

      ⑦友元运算符的参数规则与类成员运算符的参数规则不同,一员运算符必须显示地声明一个参数,二员运算符必须显示地声明两个参数。类成员运算符重载时,参数中隐含了一个this指针。

3、 实例代码
      1)下面是重载函数作为友元函数的形式

#include <iostream>
using std::cout;
using std::endl;
/*********************************************************/
/* 声明复数类
/*********************************************************/
class Complex
{
public:
    //Attribute
    int x;
    int y;
    //Operator
    void SetX(int a){x=a;}
    void SetY(int b){y=b;}

    friend Complex operator +(Complex &, Complex &);
    friend Complex operator -(Complex &, Complex &);
    friend Complex operator *(Complex &, Complex &);
    friend Complex operator /(Complex &, Complex &);

    friend Complex operator ++(Complex &);//前置方式
    friend Complex operator ++(Complex &, int);//后置方式
};
// "+"重载运算符
Complex operator +(Complex& temp1,Complex& temp2 )
{
    Complex ret;
    ret.x=temp1.x+temp2.x;
    ret.y=temp1.y+temp2.y;
    return ret;
}
// "-"重载运算符
Complex operator -(Complex& temp1,Complex& temp2 )
{
    Complex ret;
    ret.x=temp1.x-temp2.x;
    ret.y=temp1.y-temp2.y;
    return ret;
}
// "*"重载运算符
Complex operator *(Complex& temp1,Complex& temp2 )
{
    Complex ret;
    ret.x=temp1.x*temp2.x;
    ret.y=temp1.y*temp2.y;
    return ret;
}
// "/"重载运算符
Complex operator /(Complex& temp1,Complex& temp2 )
{
    Complex ret;
    ret.x=temp1.x/temp2.x;
    ret.y=temp1.y/temp2.y;
    return ret;
}
// "++"前置运算符
Complex operator ++(Complex& temp1)
{
    temp1.x=temp1.x+1;
    temp1.y=temp1.y+1;
    return temp1;
}
// "++"后置运算符
Complex operator ++(Complex& temp1,int)
{
    temp1.x=temp1.x++;
    temp1.y=temp1.y++;
    return temp1;
}
//主函数()
int main()
{
    Complex Complex1;
    Complex Complex2;
    Complex Ret;

    Complex1.SetX(30);
    Complex1.SetY(40);

    Complex2.SetX(10);
    Complex2.SetY(20);

    cout<<"重载加法运算"<<endl;
    Ret=Complex1+Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"重载减法运算"<<endl;
    Ret=Complex1-Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"重载乘法运算"<<endl;
    Ret=Complex1*Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"重载除法运算"<<endl;
    Ret=Complex1/Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"前置++运算"<<endl;
    Ret=++Complex1;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"后置++运算"<<endl;
    Ret=Complex1++;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;
    return 0;
}

javascript 运算符重载 运算符重载operator_运算符_02


2)下面是重载函数作为成员函数的实例代码:

#include <iostream>
using std::cout;
using std::endl;
/*********************************************************/
/* 声明复数类
/*********************************************************/
class Complex
{
public:
    //Attribute
    int x;
    int y;
    //Operator
    void SetX(int a){x=a;}
    void SetY(int b){y=b;}
    //成员函数
    Complex operator +(Complex &);
    Complex operator -(Complex &);
    Complex operator *(Complex &);
    Complex operator /(Complex &);
    Complex& operator ++();//前置方式
    Complex& operator ++(int);//后置方式
};
// "+"重载运算符
Complex Complex::operator +(Complex& temp1)
{
    Complex ret;
    ret.x=x+temp1.x;
    ret.y=y+temp1.y;
    return ret;
}
// "-"重载运算符
Complex Complex::operator -(Complex& temp1)
{
    Complex ret;
    ret.x=x-temp1.x;
    ret.y=y-temp1.y;
    return ret;
}
// "*"重载运算符
Complex Complex::operator *(Complex& temp1)
{
    Complex ret;
    ret.x=x*temp1.x;
    ret.y=y*temp1.y;
    return ret;
}
// "/"重载运算符
Complex Complex::operator /(Complex& temp1)
{
    Complex ret;
    ret.x=x/temp1.x;
    ret.y=y/temp1.y;
    return ret;
}
// "++"前置运算符
Complex& Complex::operator ++()
{
    x=x+1;
    y=y+1;
    return *this;
}
// "++"后置运算符
Complex& Complex::operator ++(int)
{
    x=x++;
    y=y++;
    return *this;
}
//主函数()
int main()
{
    Complex Complex1;
    Complex Complex2;
    Complex Ret;

    Complex1.SetX(30);
    Complex1.SetY(40);

    Complex2.SetX(10);
    Complex2.SetY(20);

    cout<<"重载加法运算"<<endl;
    Ret=Complex1+Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"重载减法运算"<<endl;
    Ret=Complex1-Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"重载乘法运算"<<endl;
    Ret=Complex1*Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"重载除法运算"<<endl;
    Ret=Complex1/Complex2;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"前置++运算"<<endl;
    Ret=++Complex1;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;

    cout<<"后置++运算"<<endl;
    Ret=Complex2++;
    cout<<"Ret.x="<<Ret.x<<endl;
    cout<<"Ret.y="<<Ret.y<<endl;
    return 0;
}

javascript 运算符重载 运算符重载operator_运算符_03


javascript 运算符重载 运算符重载operator_赋值运算符_04

}输出结果如下图所示:

javascript 运算符重载 运算符重载operator_javascript 重载运算符_05