C++重载>>,<<,<,<=,>,>=

>>,<<,<,<=,>,>=的重载 一般分为两类

1.声明为友元函数,用法为 cout << p
2.声明为成员函数,用法为 p << count

#include <iostream>
using namespace std;
class Point3d
{
private:
    double x;
    double y;
    double z;
public:
    Point3d(double tx, double ty, double tz);
    /// 重载为友元函数时,表示为其友元类重载运算符,其第一个参数为友元类对象的引用,
    friend ostream&
    operator <<(ostream& os, const Point3d &pt);
    friend istream&
    operator >>(istream& os, Point3d &pt);
    inline ostream&
    operator <<(ostream& os){
        os<<"inline method, "<<"x: "<<x<<", y: "<<y<<", z: "<<z;
        return os;
    }
    inline istream&
    operator >> ( istream &os){
        os >> x >> y >> z;
        std::cout << "inline "<<std::endl;
        return os;
    }
};
ostream&
operator <<(ostream& os, const Point3d &pt){
    os<<"friend method, "<<"x: "<<pt.x<<", y: "<<pt.y<<", z: "<<pt.z;
    return os;
}
istream&
operator >>(istream& os, Point3d &pt){
    os >> pt.x >> pt.y >> pt.z;
    std::cout << "friend"<< std::endl;
    return os;
}
Point3d::Point3d(double tx, double ty, double tz){
    x=tx;
    y=ty;
    z=tz;
}
int main(void){
    Point3d a(1,2,3);
    cout<<a<<endl;
    a<<cout<<endl;
    cin>>a;
    a>>cin;
}

LChar的重载就能满足 'A'<= object && object <= 'Z'
其中 'A<= object 是调用的友元函数,视为char重载了对LChar的小于等于
object<='Z'是调用的成员函数,视为LChar 重载了对char的小于等于

    struct LChar {
        LChar()
            :ch_(0u)
        {}
        LChar(char ch)
            :ch_( uint32_t(unsigned char(ch)))
        {}
        LChar(uint32_t ch)
            :ch_(ch) 
        {}
        LChar(LChar const &ch)
            :ch_(ch.ch_)
        {}

        LChar& operator=(char ch) {
            ch_ = uint32_t(unsigned char(ch));
            return *this;
        }
        LChar& operator=(uint32_t ch) {
            ch_ = ch;
            return *this;
        }
        LChar& operator=(LChar const &ch)
        {
            ch_ = ch.ch_;
            return *this;
        }

        bool operator ==(LChar const & ch) const {
            return ch_ == ch.ch_;
        }
        bool operator != (LChar const & ch) const {
            return !(*this == ch);
        }
        bool operator<(LChar const &ch) const {
            return ch_ < ch.ch_;
        }
        bool operator<=(LChar const & ch) const {
            return ch_ <= ch.ch_;
        }
        bool operator>(LChar const &ch) const {
            return ch_ > ch.ch_;
        }
        bool operator>=(LChar const &ch) const {
            return ch_ >= ch.ch_;
        }


        bool operator==(char ch) const {
            return *this == LChar(ch);
        }
        bool operator != (char ch) const {
            return *this != LChar(ch);
        }
        bool operator<(char ch) const {
            return *this < LChar(ch);
        }
        bool operator <= (char ch) const {
            return *this <= LChar(ch);
        }
        bool operator > (char ch) const {
            return *this > LChar(ch);
        }
        bool operator >= (char ch) const {
            return *this >= LChar(ch);
        }

        bool operator==(uint32_t ch) const {
            return *this == LChar(ch);
        }
        bool operator != (uint32_t ch) const {
            return *this != LChar(ch);
        }
        bool operator<(uint32_t ch) const {
            return *this < LChar(ch);
        }
        bool operator <= (uint32_t ch) const {
            return *this <= LChar(ch);
        }
        bool operator > (uint32_t ch) const {
            return *this > LChar(ch);
        }
        bool operator >= (uint32_t ch) const {
            return *this >= LChar(ch);
        }
        friend bool operator!=( char ch, LChar const &lc);
        friend bool operator==(char ch, LChar const &lc);
        friend bool operator<(char ch, LChar const & lc);
        friend bool operator<=(char ch , LChar const & lc);
        friend bool operator>(char ch, LChar const & lc);
        friend bool operator>=(char ch, LChar const & lc);

        friend bool operator!=(uint32_t ch, LChar const & lc);
        friend bool operator==(uint32_t ch, LChar const & lc);
        friend bool operator<(uint32_t ch, LChar const & lc);
        friend bool operator<=(uint32_t ch, LChar const & lc);
        friend bool operator>(uint32_t ch, LChar const & lc);
        friend bool operator>=(uint32_t ch, LChar const & lc);
        friend std::ostream& operator<<(std::ostream & os, LChar const & ch);
        friend std::istream& operator>>(std::istream& is, LChar  & ch);
    private:
        uint32_t ch_;
    };

    inline bool operator!=( char ch, LChar const &lc) {
        return LChar(ch) == lc;
    }
    inline bool operator==(char ch, LChar const &lc) {
        return LChar(ch) == lc;
    }
    inline bool operator<(char ch, LChar const &lc) {
        return LChar(ch) < lc;
    }
    inline bool operator<=(char ch, LChar const &lc) {
        return LChar(ch) <= lc;
    }
    inline bool operator>(char ch, LChar const &lc) {
        return LChar(ch) > lc;
    }
    inline bool operator>=(char ch, LChar const &lc) {
        return LChar(ch) >= lc;
    }

    inline bool operator!=(uint32_t ch, LChar const &lc) {
        return LChar(ch) != lc;
    }
    inline bool operator==( uint32_t ch, LChar const &lc) {
        return LChar(ch) == lc;
    }
    inline bool operator<(uint32_t ch, LChar const &lc) {
        return LChar(ch) < lc;
    }
    inline bool operator<=(uint32_t ch, LChar const &lc) {
        return LChar(ch) <= lc;
    }
    inline bool operator>(uint32_t ch, LChar const &lc) {
        return LChar(ch) > lc;
    }
    inline bool operator>=(uint32_t ch, LChar const &lc) {
        return LChar(ch) >= lc;
    }

    inline std::ostream& operator<<(std::ostream & os, LChar const & ch) {
        os << ch.ch_;
        return os;
    }
    inline std::istream& operator>>(std::istream& is, LChar  & ch) {
        is >> ch.ch_;
        return is;
    }

从c++重载运算符来看: z=x op y 可视为 z = x operator op ( y) 或者 z = oprator op (x,y)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

  • 什么叫重载操作符?这要先说什么是操作符,其实最简单的:+、-、*、/、<<就都是操作符,这些也是很常见的重载操作符...
    Cloudox_阅读 283评论 0 0
  • 形式:ostream& operator<<(ostream& cout,const Point& p)或 ist...
    海之梦17阅读 460评论 0 0
  • 在包装socket API的过程中,突然发现有个问题,那就是重载可能造成歧义,参考下列简化后的代码 我的目的是让s...
    哈莉_奎茵阅读 747评论 0 1
  • 技术交流QQ群:1027579432,欢迎你的加入! 一.static关键字的作用 1.静态成员的特点 1.sta...
    CurryCoder阅读 3,046评论 3 3
  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 4,481评论 0 13

友情链接更多精彩内容