CPP-operator==, <overloading

这篇博客探讨了在C++中如何利用已有的运算符重载来实现其他相关操作符。一旦定义了`operator==`,可以轻松实现`operator!=`,只需调用`operator==`并取反。同样,定义了`<`后,可以方便地定义`>`。标准库提供了一些模板帮助实现这些关系操作符。文章通过为数组类型定义`<`运算符为例,展示了如何实现这些操作,并提到了在成员函数中使用`this`指针的细节。然而,在尝试使用非常量成员函数(如lengthsquare())时,由于`operator<`是常量成员函数,导致了编译错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果已经定义了operator==,那么operator!=很容易实现,只要调用operator==,然后取反。

bool Array::operator!=(const Array& v) const
{
    return !((*this) == v);
}

这个函数的形式实际上和具体的类无关。即如果定义了operator==,那么总能根据这个模式定义operatot!=. 同样,如果定义了<, 则很容易能够定义>. 如果定义了==,则结合<和==,则能够定义<=和>=. 标准文件util包含了一系列预先定义的这些关系操作符的模式。使用时只需定义operator==, operator<,则自动有operator!=, operator>, operator<=.因此从不需要定义多余两个比较操作符。看以下例子:为array定义<.

bool operator< (const Array& v) const;

实现:

bool Array::operator< (const Array& v) const
{
    int n; //the length of the shortest of this object and v
    if(num <v.num)
    n = num;
    else
    n = v.num;
    for(int i=0;i<n;i++)
    if(p[i] <v.p[i])
    return true;
    else if(p[i]>v.p[i])
    return false;
    return num <v.num; //equal length
}

为了自动使用别的比较操作符。需要在类定义文件中包含以下信息:

#include〈utility>
using namespace std::rel_ops;

单操作符:operator-

Array operator- () const;

Array Array::operator- () const
{
Array temp(*this); //creat a copy of this array object
for(int i=0;i

class Myclass{
public:
    bool operator== (const Myclass &vb) const {
            if(this->vx == vb.vx && this->vy == vb.vy && this->vz == vb.vz) return true;
            else return false;};
private:
    double  vx,vy,vz;
}

其实当在一个成员函数内部写一个成员名字时候(如vx),编译器将解释为this->vx。因此以上语句其实不需要明确指出this->vx;故代码可以简化:

class Myclass{
public:
    bool operator== (const Myclass &vb) const {
        return(vx == vb.vx && vy == vb.vy && vz == vb.vz) };
private:
    double  vx,vy,vz;
}

接下来实现矢量大小比较(长度)

bool operator<  (const Vector3d &vb) const {return (this->lengthsquare() < vb.lengthsquare());};

inline double lengthsquare() {return(vx*vx + vy*vy + vz*vz);};

这几句总是给错误提示:
“the object has type qualifiers that are not compatible with the member function”,

因为operator< 声明为一个常函数,而函数lengthsquare()没有声明为常函数。

bool operator<  (const Vector3d &vb) const {return (this->lengthsquare() < vb.lengthsquare());};

inline double lengthsquare() const{return(vx*vx + vy*vy + vz*vz);};

其他comparison operator:

bool operator== (const Vector3d &vb) const {return(vx == vb.vx && vy == vb.vy && vz == vb.vz);}
        bool operator!= (const Vector3d &vb) const {return !(*this == vb);};
        bool operator<  (const Vector3d &vb) const {return (this->lengthsquare() < vb.lengthsquare());};
        bool operator>= (const Vector3d &vb) const {return !(*this < vb);};
        bool operator>  (const Vector3d &vb) const {return (*this >= vb) && (*this != vb);};
        bool operator<= (const Vector3d &vb) const {return !(*this > vb);};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值