zoukankan      html  css  js  c++  java
  • 运算符重载总结(大全)

    #include <iostream>
    //#include <string>
    #include <sstream>
    //#include <stdbool.h>
    #include <stdexcept>
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    class A
    {
        public:
            A();
            A(int initialValue);
            A(const A& src);
            
            //友元函数是全局函数 ,返回的是对象本身,不是引用 
            friend A operator +(const A& lhs,const A& rhs);
            friend A operator -(const A& lhs,const A& rhs);
            friend A operator *(const A& lhs,const A& rhs);
            friend A operator /(const A& lhs,const A& rhs);
            // 局部函数,简写运算符
            A& operator = (const A& rhs);
    //        A& operator +(const A& rhs);
    //        A& operator -(const A& rhs);
    //        A& operator *(const A& rhs);
    //        A& operator /(const A& rhs);
            //局部函数,简写运算符 
            A& operator +=(const A&rhs);
            A& operator -=(const A&rhs);
            A& operator *=(const A&rhs);
            A& operator /=(const A&rhs);
            //比较运算符
            friend bool operator ==(const A& lhs,const A& rhs);
            friend bool operator !=(const A& lhs,const A& rhs);
            friend bool operator >=(const A& lhs,const A& rhs);
            friend bool operator <=(const A& lhs,const A& rhs);
            friend bool operator >(const A& lhs,const A& rhs);
            friend bool operator <(const A& lhs,const A& rhs);
            //普通成员函数 
            void setValue(int value);
            void show();
             
        private:
            int mValue;        
    };
    A::A()
    :mValue(0)
    {
        
    }
    A::A(int initialValue)
    :mValue(initialValue)
    {
        
    }
    A::A(const A& src)
    :mValue(src.mValue)
    {
        
    }
    void A::setValue(int value)
    {
        mValue = value;
    }
     
    A operator +(const A& lhs,const A& rhs)
    {
        std::cout << "AAA" << std::endl;    //检测语句 
        A newA;
        newA.setValue(lhs.mValue + rhs.mValue);
        return newA;
    }
    A operator -(const A& lhs,const A& rhs)
    {
        std::cout << "BBB" << std::endl;    //检测语句 
        A newA;
        newA.setValue(lhs.mValue - rhs.mValue);
        return newA;
    }
    A operator *(const A& lhs,const A& rhs)
    {
        std::cout << "CCC" << std::endl;    //检测语句 
        A newA;
        newA.setValue(lhs.mValue * rhs.mValue);
        return newA;
    }
    A operator /(const A& lhs,const A& rhs)
    {
        std::cout << "DDD" << std::endl;    //检测语句 
        if(!rhs.mValue)
        {
            throw std::invalid_argument("denominator is zero
    ");
        }
        A newA;
        newA.setValue(lhs.mValue / rhs.mValue);
        return newA;
    }
    
    A& A::operator = (const A& rhs)
    {
        std::cout << "EEE" << std::endl;    //检测语句 
        if(this == &rhs)
        {
            std::cout << "自赋值" << std::endl; 
            return *this;
        }
        this->mValue = rhs.mValue;
        return * this;
    }
    //A& A::operator +(const A& rhs)
    //{
    //    std::cout << "!!!!!" << std::endl;    //检测语句 
    //    this->setValue(this->mValue + rhs.mValue);
    //    return *this;
    //}
    //A& A::operator -(const A& rhs)
    //{
    //    std::cout << "@@@@@" << std::endl;    //检测语句 
    //    this->setValue(this->mValue - rhs.mValue);
    //    return *this;
    //}
    //A& A::operator *(const A& rhs)
    //{
    //    std::cout << "#####" << std::endl;    //检测语句 
    //    this->setValue(this->mValue + rhs.mValue);
    //    return *this;
    //} 
    //A& A::operator /(const A& rhs)
    //{
    //    std::cout << "¥¥¥¥" << std::endl;    //检测语句 
    //    if(!rhs.mValue)
    //    {
    //        throw std::invalid_argument("denominator is zero
    ");
    //    }
    //    this->setValue(this->mValue / rhs.mValue);
    //    return *this;
    //}
    A& A::operator +=(const A&rhs)
    {
        std::cout << "FFF" << std::endl;    //检测语句 
        this->setValue(this->mValue + rhs.mValue);
        return *this;
    }
    A& A::operator -=(const A&rhs)
    {
        std::cout << "GGG" << std::endl;    //检测语句 
        this->setValue(this->mValue - rhs.mValue);
        return *this;
    }
    A& A::operator *=(const A&rhs)
    {
        std::cout << "HHH" << std::endl;    //检测语句 
        this->setValue(this->mValue * rhs.mValue);
        return *this;
    }
    A& A::operator /=(const A&rhs)
    {
        std::cout << "III" << std::endl;    //检测语句 
        if(!rhs.mValue)
        {
            throw std::invalid_argument("denominator is zero!
    ");
        }
        this->setValue(this->mValue / rhs.mValue);
        return *this;
    }
    bool operator ==(const A& lhs,const A& rhs)
    {
        std::cout << "QQQ" << std::endl;    //检测语句 
        return lhs.mValue == rhs.mValue;
    }
    bool operator !=(const A& lhs,const A& rhs)
    {
        std::cout << "PPP" << std::endl;    //检测语句 
        return lhs.mValue != rhs.mValue;
    }
    bool operator >=(const A& lhs,const A& rhs)
    {
        std::cout << "TTT" << std::endl;    //检测语句 
        return lhs.mValue >= rhs.mValue;
    } 
    bool operator <=(const A& lhs,const A& rhs)
    {
        std::cout << "OOO" << std::endl;    //检测语句 
        return lhs.mValue <= rhs.mValue;
    } 
    bool operator >(const A& lhs,const A& rhs)
    {
        std::cout << "KKK" << std::endl;    //检测语句 
        return lhs.mValue > rhs.mValue;
    }
    bool operator <(const A& lhs,const A& rhs)
    {
        std::cout << "XXX" << std::endl;    //检测语句 
        return lhs.mValue < rhs.mValue;
    }
    void A::show()
    {
        std::cout << "value : " << mValue << std::endl;
    }
    
    int main(int argc, char** argv) 
    {
        A a1(1);
        A a2(2);
        A a3(3);
        A a4(4);
        A a5,a8,a6,a7;
        a5 = a1 + a2;    //先运行operator+,后运行operator=    ,a1的mValue = 3 
        a5.show();
        
        a6 = a1 + 3;    // 先运行operator+,后运行operator=    , a1的mValue = 6
        a6.show();
        
        a7 = 5 + a1;
        a7.show();
        
        a8 = a1 + a2;
        if(a8 == a5)
        {
            std::cout << "==运算符运行!" << std::endl; 
        }else
        {
            std::cout << "==不能运算符运行!" << std::endl;
        }
        return 0;
    }

    运算的结果:

    AAA
    EEE
    value : 3
    AAA
    EEE
    value : 4
    AAA
    EEE
    value : 6
    AAA
    EEE
    QQQ
    ==运算符运行!

    这样子是我们期望的结果,但是当我们把类内注释的部分取消注释时,会发现是这样的结果!

    !!!!!
    EEE
    value : 3
    !!!!!
    EEE
    value : 6
    AAA
    EEE
    value : 11
    !!!!!
    EEE
    QQQ
    ==不能运算符运行!

    这是为什么呢?

    原因是:当实例中编译器碰到+号的时候,首先在类内的成员函数寻找,这样就找到了A& operator = (const A& rhs);

    如果是类内成员函数美誉匹配的operator+时,编译器才会接着往下找全局函数,就找到了friend A operator +(const A& lhs,const A& rhs);

    首先他是友函数,是全局函数,

  • 相关阅读:
    golang语法要点笔记
    环境配置
    实现chrome多用户独立cookie
    Golang遇到的问题记录
    php preg_replace去除html xml 注释
    C 基础
    多种写法
    mysql 查看当前数据库
    sql group by
    sql CONCAT()
  • 原文地址:https://www.cnblogs.com/boost/p/10337115.html
Copyright © 2011-2022 走看看