C++运算符重载(二)

       C++允许在自己的类中,或是在全局作用域中重定义运算符的含义。由于很多面向对象的语言没有提供这种能力,因此你可能会低估这种特性在C++中的作用。C++中运算符的概念十分广泛,甚至包含[](数组索引)、()(函数调用)、类型转换以及内存分配和释放例程。可以通过运算符重载来改变语言运算符对自定义类的行为。能让自己的类具有内建类型的类似行为,甚至可以编写看上去类似于数组、函数或指针的类。在博主的《C++运算符重载》系列博文中会对我们常用的运算符提供重载的实例,希望大家能有所收获。额,本篇博文就让我们一起来探讨一下重载运算符中的关系运算符重载吧。


         关系运算符包括“==“、“!=”、“<”、“>”、“<=”、“>=”,我们一般他们重载为全局函数。咱们举个简单的栗子吧。

//Demo.h

#pragma once

 

//定义模板类

template <typenameT>

class Demo

{

public:

    Demo(constT& value = T());

    virtual~Demo(){}

 

    //template<typename T> /*此句可有可无*/

    //友元声明中间的<T>不能省略,它用来告诉编译器友元函数也是模板

    friendbooloperator== <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendbooloperator< <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendbooloperator<= <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendbooloperator!= <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendbooloperator> <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendbooloperator>= <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendDemo<T> operator+ <T> (constDemo<T>&lhs, const Demo<T> &rhs);

 

private:

    T   m_value;

};

 

#include “Demo.inl”

 

//Demo.inl

template <typenameT>

Demo<T>::Demo(constT& value/* =T()*/)

    : m_value(value)

{

 

}

 

template <typenameT>

bool operator == (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value== rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

bool operator < (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value< rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

bool operator <= (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value<= rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

bool operator != (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value!= rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

bool operator > (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value> rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

bool operator >= (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value>= rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

Demo<T>operator + (const Demo<T> &lhs, constDemo<T>&rhs)

{

    returnDemo<T>(lhs.m_value+ rhs.m_value);

}

 

//main.cpp

#include <iostream>

#include “Demo.h”

using namespacestd;

 

int main()

{

    Demo<int>myDemo1(3);

    Demo<int>myDemo2(3);

    Demo<int>myDemo3(4);

 

    cout.setf(ios::boolalpha);

    cout<< "myDemo1 == myDemo2: "<< (myDemo1 == myDemo2) << endl;

    cout<< "myDemo1 == myDemo3: "<< (myDemo1 == myDemo3) << endl;

 

    cout<< "myDemo1 < myDemo2 : "<< (myDemo1 < myDemo2) << endl;

    cout<< "myDemo1 < myDemo3 : "<< (myDemo1 < myDemo3) << endl;

 

    cout<< "myDemo1 != myDemo2: "<< (myDemo1 != myDemo2) << endl;

    cout<< "myDemo1 != myDemo3: "<< (myDemo1 != myDemo3) << endl;

 

    cout<< "myDemo1 <= myDemo2: "<< (myDemo1 <= myDemo2) << endl;

    cout<< "myDemo1 <= myDemo3: "<< (myDemo1 <= myDemo3) << endl;

 

    cout<< "myDemo1 > myDemo2 : "<< (myDemo1 > myDemo2) << endl;

    cout<< "myDemo1 > myDemo3 : "<< (myDemo1 > myDemo3) << endl;

 

    cout<< "myDemo1 >= myDemo2: "<< (myDemo1 >= myDemo2) << endl;

    cout<< "myDemo1 >= myDemo3: "<< (myDemo1 >= myDemo3) << endl;

 

    return0;

}

 

程序运行结果:


       程序运行出来了,好开心啊,我也终于会编写关系运算符的重载了。可是上面的程序就正的好么?相信你对上面的程序不是很满意,哪怕有一点不满意就说明你是个程序猿的料啦。学过离散数学的同学一定知道,“==”和“<”就足以表示其他的关系比较了。例如,我们可以用“!operator ==”来表示“operator !=”;用“(!operator <) && (!operator ==)”来表示“operator>”。可能有的小伙伴已经晕了,每次比较还要写这么复杂的表达式,可读性也不好,还不如像上面那样把所有的关系运算符都重载一遍。虽然代码多一点,但是容易理解啊。然而这一切C++标准都帮你做了,在std::rel_ops命令空间中关系运算符包含了如下函数模板:

template <classT> booloperator != (const T&lhs, const T&rhs);

template <classT> booloperator <= (const T&lhs, const T&rhs);

template <classT> booloperator >= (const T&lhs, const T&rhs);

template <classT> booloperator > (const T&lhs, const T&rhs);

       在这些模板的实现中就帮你完成了那些复杂的表达式的编写,进而完成关系比较。是不是很方便啊,下面我们就一起来重新编写上面的栗子吧。

//Demo.h

#pragma once

 

//定义模板类

template <typenameT>

class Demo

{

public:

    Demo(constT& value = T());

    virtual~Demo(){}

 

    //template<typename T> /*此句可有可无*/

    //友元声明中间的<T>不能省略,它用来告诉编译器友元函数也是模板

    friendbooloperator== <T> (constDemo<T>&lhs, const Demo<T> &rhs);

    friendbooloperator< <T> (constDemo<T>&lhs, const Demo<T> &rhs);

 

private:

    T   m_value;

};

 

#include “Demo.inl”

 

//Demo.inl

//using namespace std::rel_ops;

using std::rel_ops::operator<=;

using std::rel_ops::operator>=;

using std::rel_ops::operator!=;

using std::rel_ops::operator>;

 

template <typenameT>

Demo<T>::Demo(constT& value/* =T()*/)

    : m_value(value)

{

 

}

 

template <typenameT>

bool operator == (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value== rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

template <typenameT>

bool operator < (constDemo<T>&lhs, constDemo<T>&rhs)

{

    boolret = false;

 

    if(lhs.m_value< rhs.m_value)

    {

        ret = true;

    }

 

    returnret;

}

 

 

//main.cpp

#include <iostream>

#include “Demo.h”

using namespacestd;

 

int main()

{

    Demo<int>myDemo1(3);

    Demo<int>myDemo2(3);

    Demo<int>myDemo3(4);

 

    cout.setf(ios::boolalpha);

    cout<< "myDemo1 == myDemo2: "<< (myDemo1 == myDemo2) << endl;

    cout<< "myDemo1 == myDemo3: "<< (myDemo1 == myDemo3) << endl;

 

    cout<< "myDemo1 < myDemo2 : "<< (myDemo1 < myDemo2) << endl;

    cout<< "myDemo1 < myDemo3 : "<< (myDemo1 < myDemo3) << endl;

 

    cout<< "myDemo1 != myDemo2: "<< (myDemo1 != myDemo2) << endl;

    cout<< "myDemo1 != myDemo3: "<< (myDemo1 != myDemo3) << endl;

 

    cout<< "myDemo1 <= myDemo2: "<< (myDemo1 <= myDemo2) << endl;

    cout<< "myDemo1 <= myDemo3: "<< (myDemo1 <= myDemo3) << endl;

 

    cout<< "myDemo1 > myDemo2 : "<< (myDemo1 > myDemo2) << endl;

    cout<< "myDemo1 > myDemo3 : "<< (myDemo1 > myDemo3) << endl;

 

    cout<< "myDemo1 >= myDemo2: "<< (myDemo1 >= myDemo2) << endl;

    cout<< "myDemo1 >= myDemo3: "<< (myDemo1 >= myDemo3) << endl;

 

    return0;

}

 

程序运行结果:


       看,是不是很方便啊,不用再去编写那么多的关系运算符重载函数了,也不用去编写那些复杂的表达式了。只需要编写operator ==和operator <关系运算符重载函数,再加上以下using语句即可:

using std::rel_ops::operator<=;

using std::rel_ops::operator>=;

using std::rel_ops::operator!=;

using std::rel_ops::operator>;

       如果你还是觉得编写那么多的using语句有点多余,想追求更简洁的版本。那么到底有没有呢?可能聪明的小伙伴已经想到了,对,直接使用命名空间不就得了。如果你忘了using的用法,那就先去《漫谈继承技术(六)》和《灵活而奇特的C++语言特性——typedef & aliases》回顾一下,博主在那里分别讲述了using的不同使用方法。

       C++运算符重载中的关系运算符重载就讲到这里,相信大家对它的概念和用法已经熟悉了吧。如果想了解更多关于C++运算符重载的知识,请关注博主的《C++运算符重载》系列博文,在那里我们将会通过程序实例去探讨C++运算符重载的魅力,相信你在那里会有不一样的收获。当然,如果你对C++很感兴趣的话,那么请关注博主的《漫谈继承技术》和《灵活而奇特的C++语言特性》系列博文,在那里你也许会发现C++更大的魅力,让你对这门博大精深的语言更加爱不释手。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值