C++面向对象编程之六:重载操作符(<<,>>,+,+=,==,!=,=)

重载操作符

C++允许我们重新定义操作符(例如:+,-,*,/)等,使其对于我们自定义的类类型对象,也能像内置数据类型(例如:int,float,double)等一样直观,可以进行加,减,乘,除,比较大小等等操作。

重载操作符本质是函数,只是这个函数的函数名比较特别,为:operator后接需要重新定义的操作符的符号。例如,重载+号,函数名为:operator+;重载-号,函数名:operator-。因为重载操作符本质是函数,所以实际上就是为某个自定义的数据类类型或枚举类型实现函数重载,比如内置int类型已经有

int operator+(int, int)版本,我们有一个自定义的complex(复数类),要想跟内置的int类型有一样直观的操作两个复数相加,只要我们为自定义的complex(复数类)重定义操作符+,提供complex operator+(complex, complex)版本即可。

重载操作符语法
返回值类型 operator操作符号(形参列表)
{
}

重载操作符的两种形式

对于大多数重载操作符来说,可以定义为全局函数或类的成员函数。

1.重载操作符为全局函数,并且通常必须将这个全局函数设置为所操作类的友元

#include <iostream>
using namespace std;

class MyInt
{
    friend MyInt operator+(const MyInt &a, const MyInt &b); //这个函数为MyInt类的友元
    public:
    MyInt():m_num(0)
    {
    }
    MyInt(const int num):m_num(num)
    {
    }

    int getNum() const
    {
        return m_num;
    }

    private:
    int m_num;
};

//重载操作符为全局函数
MyInt operator+(const MyInt &a, const MyInt &b)
{
    MyInt temp;
    temp.m_num = a.m_num + b.m_num; //如果没有把这个全局函数设置为操作类MyInt的友元,则不能直接访问MyInt类的
                                    //私有成员变量m_num
    return temp;
}

int main(int argc, char *argv[])
{
    MyInt a(1);
    MyInt b(2);
    MyInt c = a + b; //本质是:调用了全局函数,即 MyInt c = operator+(a, b);
    MyInt d = operator+(a, b); 

    cout << "a + b = " << c.getNum() << endl;
    cout << "operator+(a, b) = " << d.getNum() << endl;

    return 0;
}

注意:重载操作符为全局函数时,本质是调用了该全局函数。例如:MyInt c = a + b; //本质是:调用了全局函数,即 MyInt c = operator+(a, b);

2.重载操作符为类的成员函数

#include <iostream>
using namespace std;

class MyInt
{
    public:
    MyInt():m_num(0)
    {
    }
    MyInt(const int num):m_num(num)
    {
    }

    int getNum() const
    {
        return m_num;
    }

    MyInt operator+(const MyInt &b) //重载操作符为类成员函数
    {
        MyInt temp;
        temp.m_num = this->m_num + b.m_num;
        return temp;
    }

    private:
    int m_num;
};

int main(int argc, char *argv[])
{
    MyInt a(1);
    MyInt b(2);
    MyInt c = a + b; //本质是:调用了MyInt类的成员函数,即 MyInt c = a.operator+(b);
    MyInt d = a.operator+(b);

    cout << "a + b = " << c.getNum() << endl;
    cout << "a.operator+(b) = " << d.getNum() << endl;

    return 0;
}

注意:重载操作符为类的成员函数时,本质是调用了该类的成员函数。例如:MyInt c = a + b; //本质是:调用了MyInt类的成员函数,即 MyInt c = a.operator+(b);

重载操作符为全局函数和重载操作符为类的成员函数的形参区别

1.对于重载操作符为全局函数和重载操作符为类的成员函数,形参的个数看上去是有区别的,一般重载操作符为类的成员函数,其形参个数看起来比重载操作符为全局函数的形参个数少1个,实际上,重载操作符为类的成员函数,这个成员函数有一个隐含的this指针形参,限定为操作符的第一个操作数,所以对于编译器而言。形参个数并没有少1个的。而重载操作符为全局函数,这个全局函数的第一个形参则为操作符的第一个操作数

2.对于重载操作符函数,形参是有顺序的,例如:cout << "Hello World\n";这句是正常的输出操作符<< 打印的语法:第一个操作数的实参为:cout,第二个操作数的实参为:"Hello World\n",那么正确的全局函数定义为:ostream& operator<<(ostream& cout, const string &str);但是如果我们将全局函数定义为:ostream& operator<<(const string &strostream& cout);函数体内我们同样实现功能正常打印,我们用函数调用法:operator("Hello World\n", cout);也能在屏幕中正确打印:Hello World。看起来也没有错。但简化的调用版本形参是有顺序的,第一操作数为函数的第一个形参,第二个操作数为函数的第二个形参。所以简化的调用版本为:"Hello World\n" << cout;这就是错误的了,所以对于重载操作符函数,形参是有顺序的

重载操作符为全局函数还是重载操作符为类的成员函数,应该怎么选择?

对于重载操作符可以为一个普通的全局函数或一个类的成员函数这两种方式来实现,我的建议是,对于能够使用类的成员函数来实现的,我们应该选择用类的成员函数来实现,因为选择用普通的全局函数来实现,需要把这个全局函数设置为操作类的友元,从而达到可以直接访问该操作类的非共有成员变量的目的,但这破坏类的封装性。

不能重载的操作符
.         :类的对象访问操作符
.*  ->*   :类的对象或或类的对象指针访问类中的函数指针操作符
::        :域操作符
?=        :条件操作符
sizeof    :长度操作符
#         :预处理操作符         
重载操作符可以给我们自定义的类类型对象的进行操作符运算的时候更加直观。但不要滥用重载操作符。

1.不能改变内置类型的操作符的含义。例如:int operator+(int, int)

2.不能为内置类型定义额外的新的操作符。例如:不能定义两个数组为操作数的operator+

3.逗号(,),取地址(&),逻辑与(&&),逻辑或(||)这些操作符具有有用的内置含义,不建议重载。

假设我们有一个MyInt类,实现的功能跟内置的类型int一样。那么下面我们为这个MyInt类实现一下相关的操作符重载吧。

重载输出操作符<<

分析:

对于内置的类型int
int a = 0;
cout << a << endl;
<< 操作符有两个操作数,
第一个(左)操作数数据类型为:ostream&,
第二个(右)操作数数据类型为:const int&
所以我们只能重载输出操作符为全局函数,不能为MyInt类的成员函数,
因为重载操作符为类的成员函数的第一个操作数数据类型限定为this指针,
而我们需要的是第一个操作数数据类型为:ostream&,
因为输出a后,后面的操作是还能输出换行的,所以返回值类型为ostream&

经过以上分析,我们可以得出,重载输出操作符<<的全局函数为:

ostream& operator<<(ostream &cout, const MyInt a)

#include <iostream>
using namespace std;

class MyInt
{
    friend ostream& operator<<(ostream &cout, const MyInt &a);
    public:
    MyInt():m_num(0)
    {

    }
    MyInt(const int num):m_num(num)
    {

    }

    private:
    int m_num;
};

ostream& operator<<(ostream &cout, const MyInt &a)
{
    cout << a.m_num;
    return cout;
}

int main(int argc, char *argv[])
{
    MyInt a;
    cout << a << endl;

    return 0;
}

重载输入操作符>>

分析:

对于内置的类型int
int a = 0;
int b = 0;
cin >> a >> b;
>> 操作符有两个操作数,
第一个(左)操作数数据类型为:istream&,
第二个(右)操作数数据类型为:const int&
所以我们只能重载输出操作符为全局函数,不能为MyInt类的成员函数,
因为重载操作符为类的成员函数的第一个操作数数据类型限定为this指针,
而我们需要的是第一个操作数数据类型为:istream&,
因为输入a后,后面的操作是还能输入b的,所以返回值类型为istream&

经过以上分析,我们可以得出,重载输入操作符>>的全局函数为:

istream& operator>>(istream &cin, MyInt &a)

#include <iostream>
using namespace std;

class MyInt
{
    friend ostream& operator<<(ostream &cout, const MyInt &a);
    friend istream& operator>>(istream &cin, MyInt &a);
    public:
    MyInt():m_num(0)
    {

    }
    MyInt(const int num):m_num(num)
    {

    }

    private:
    int m_num;
};

ostream& operator<<(ostream &cout, const MyInt &a)
{
    cout << a.m_num;
    return cout;
}

istream& operator>>(istream &cin, MyInt &a)
{
    cin >> a.m_num;
    return cin;
}

int main(int argc, char *argv[])
{
    MyInt a;
    MyInt b;
    cout << "请输入a和b的值,用空格隔开:" << endl;
    cin >> a >> b;

    cout << "a = " << a << ",b = " << b << endl;

    return 0;
}
算术运算符和关系运算符

一般来说,我们应该将算术操作符和关系操作符定义为非成员函数。这是因为,算术操作符和关系操作符中,例如:有左操作数为int a = 1,右操作数为double b = 2.12345,进行 a + b运算时,左操作数a会转成跟右操作数b的精度一样的double类型,即:a + b = 1.000000000000000(双精度保留15位小数)+ 2.123450000000000(双精度保留15位小数),如果将算术操作符和关系操作符定义定义为类的成员函数时,左操作数限定为隐含的this指针,就违背了在C/C++中,如果一个表达式中含有不同类型的常量或变量,在计算时,会将它们自动转换为精度更高的同一种数据类型。

重载相加操作符+

1.返回值类型为:MyInt,因为两个数相加,是不能改变左操作数或右操作数的值的,所以返回值类型为MyInt,而不是MyInt&

2.重载相加操作符+的非成员函数为:MyInt operator+(const MyInt &a, const MyInt &b)

#include <iostream>
using namespace std;

class MyInt
{
    friend ostream& operator<<(ostream &cout, const MyInt &a);
    friend istream& operator>>(istream &cin, MyInt &a);
    friend MyInt operator+(const MyInt &a, const MyInt &b);
    public:
    MyInt():m_num(0)
    {

    }
    MyInt(const int num):m_num(num)
    {

    }

    private:
    int m_num;
};

ostream& operator<<(ostream &cout, const MyInt &a)
{
    cout << a.m_num;
    return cout;
}

istream& operator>>(istream &cin, MyInt &a)
{
    cin >> a.m_num;
    return cin;
}

MyInt operator+(const MyInt &a, const MyInt &b)
{
    return MyInt(a.m_num + b.m_num);
}

int main(int argc, char *argv[])
{
    MyInt a(1);
    MyInt b(2);
    cout << "a + b = " << a + b << endl;

    return 0;
}
重载复合赋值操作符+=

1.返回值类型为:MyInt&,因为x += y;是要改变左操作数x,并且返回的还是被修改后的x的

2.重载复合赋值操作符+=的非成员函数为:MyInt& operator+=(MyInt &a, const MyInt &b)

#include <iostream>
using namespace std;

class MyInt
{
    friend ostream& operator<<(ostream &cout, const MyInt &a);
    friend istream& operator>>(istream &cin, MyInt &a);
    friend MyInt& operator+=(MyInt &a, const MyInt &b);
    public:
    MyInt():m_num(0)
    {

    }
    MyInt(const int num):m_num(num)
    {

    }

    private:
    int m_num;
};

ostream& operator<<(ostream &cout, const MyInt &a)
{
    cout << a.m_num;
    return cout;
}

istream& operator>>(istream &cin, MyInt &a)
{
    cin >> a.m_num;
    return cin;
}

MyInt& operator+=(MyInt &a, const MyInt &b)
{
    a.m_num += b.m_num;
    return a;
}

int main(int argc, char *argv[])
{
    MyInt a(1);
    MyInt b(2);
    a += b;
    cout << "after a += b, a = " << a << endl;

    return 0;
}
重载相等操作符==,不等操作符-=

1.如果类中有一个操作,是确认该类中的两个对象是否相等的,我们应该将该函数名定义为operator==,而不是定义一个命名函数。因为我们更习惯于用==操作符来比较两个对象是否相等。

2.对于一个类,如果我们定义了operator==,也应该定义operator!=。

3.定义了operator==的类更容易与标准库一起使用。例如:find,默认使用了==操作符,如果定义了==,那么这么算法可以无须任何特殊处理而用于该类类型。

#include <iostream>
using namespace std;

class MyInt
{
    friend ostream& operator<<(ostream &cout, const MyInt &a);
    friend istream& operator>>(istream &cin, MyInt &a);
    friend bool operator==(const MyInt &a, const MyInt &b);
    friend bool operator!=(const MyInt &a, const MyInt &b);
    public:
    MyInt():m_num(0)
    {

    }
    MyInt(const int num):m_num(num)
    {

    }

    private:
    int m_num;
};

ostream& operator<<(ostream &cout, const MyInt &a)
{
    cout << a.m_num;
    return cout;
}

istream& operator>>(istream &cin, MyInt &a)
{
    cin >> a.m_num;
    return cin;
}

bool operator==(const MyInt &a, const MyInt &b)
{
    return a.m_num == b.m_num;
}

bool operator!=(const MyInt &a, const MyInt &b)
{
    return a.m_num != b.m_num;
}

int main(int argc, char *argv[])
{
    MyInt a(1);
    MyInt b(2);
    MyInt c(1);
    if (a == b)
        cout << "a == b" << endl;
    else 
        cout << "a != b" << endl;
    if (a == c)
        cout << "a == c" << endl;
    else 
        cout << "a != c" << endl;

    if (a != b)
        cout << "a != b" << endl;
    else 
        cout << "a == b" << endl;
    if (a != c)
        cout << "a != c" << endl;
    else 
        cout << "a == c" << endl;

    return 0;
}
重载赋值操作符=

1.C++是允许类类型对象给同类型的其他对象赋值的,具体可查看我写的这篇文章里利用等号法创建对象的介绍C++面向对象编程之二:构造函数、拷贝构造函数、析构函数,这里不再累赘。类赋值操作符形参的数据类型为该类类型,通常形参为该类类型的const引用,或该类的非const引用或类类型或更多的其他数据类型(例如:MyInt类中,那么该类的赋值操作符形参可为const MyInt &,或MyInt &或MyInt)。如果我们没有定义赋值操作符=,那么编译器将为该类提供一个。所以,类赋值操作符=必须是类成员函数,以便编译器可以知道是否需要为该类提供一个。

2.重载赋值操作符=的返回值类型必须是*this的引用(即左操作符的引用),这样子返回是跟内置数据类型的赋值是一致的。因为赋值返回*this的引用,这样子就不需要创建一个该类的一个临时对象,然后将临时对象返回,然后再调用该类的拷贝构造函数,将该临时对象的值拷贝后,又释放该临时对象,这一系列的系统开销,从而提高代码的执行效率。

#include <iostream>
using namespace std;

class MyInt
{
    friend ostream& operator<<(ostream &cout, const MyInt &a);
    friend istream& operator>>(istream &cin, MyInt &a);
    public:
    MyInt():m_num(0)
    {

    }
    MyInt(const int num):m_num(num)
    {

    }

    MyInt& operator=(const MyInt &b)
    {
        m_num = b.m_num;
        return *this;
    }

    private:
    int m_num;
};

ostream& operator<<(ostream &cout, const MyInt &a)
{
    cout << a.m_num;
    return cout;
}

istream& operator>>(istream &cin, MyInt &a)
{
    cin >> a.m_num;
    return cin;
}

int main(int argc, char *argv[])
{
    MyInt a(1);
    MyInt b = a;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

好了,关于重载操作符,篇幅比较长,本文对重载操作符(<<,>>,+,+=,==,!=,=)进行了讲解,还有一些比较重要的操作符,比如前++,后++,前--,后--,下标操作符[]等,还没有介绍,就放到下一篇博文吧!

  • 1
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值