• 【C++编程语言】之 加号 左移 递增 赋值 关系 函数调用 的 运算符重载


    1.加号运算符重载

    ​ 运算符重载概念:对已有的运算符进行定义,赋予另外一种功能,以适应不同的数据类型。运算符重载,也可以发生函数重载

    class Person{
        public:
            int m_A;
            int m_B;
        Person PersonAdd(Person &p){
            Person temp;
            temp,m_A = this.m_A + p.m_A;
            temp,m_B = this.m_B + p.m_B;
        }
        //1.成员函数重载+号
         Person operator+(Person &p){
            Person temp;
            temp,m_A = this.m_A + p.m_A;
            temp,m_B = this.m_B + p.m_B;
        }
    };
    //2.全局函数重载+号
    Person operator+(Person &p1,Person &p2){
            Person temp;
            temp,m_A = p1.m_A + p2.m_A;
            temp,m_B = p1.m_B + p2.m_B;
        }
    void test01(){
        Person p1;
        p1.m_A = 10;
        p1.m_B = 10;
        Person p2;
        P2.m_A = 10;
        p2.m_B = 10;
        Person p3 = p1.PersonAdd(p2);//这个可以实现成员函数的相加。
        //但是系统提供一个专门重载+号的函数名operator+这样可以实现简化
         //1.成员函数重载+号
         Person p3 = p1.operator+(p2);
         //可以简化为
         Person p3 = p1+p2;
         //2.全局函数重载+号
         Person p3 =operator+(p1.p2);
         //可以简化为
         Person p3 = p1+p2;
        //运算符重载,也可以发生函数重载。
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    总结:

    ​ 1.对于内置的数据类型的表达式的运算符是不可以改变的

    ​ 2.不要滥用运算符重载

    2.左移运算符重载

    ​ 作用:可以输出自定义数据类型。

    问题:

    int a = 10;
    cout << a <<endl;//可以输出
    class Person{
        public:
            int m_A;
            int m_B;
    };
    Person p;
    p.m_A = 10;
    P.m_B = 20;
    cout << p <<endl;//无法输出
    //这个是就可以从在左移运算符<<让它输出自定义的数据类型。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    解答

    class Person{
        public:
            int m_A;
            int m_B;
        	//利用成员函数重载 左移运算符 是无法实现的 
        	//只能使用全局函数重载左移运算符
        	void operator<<(){}
    };
    //利用全局函数重载左移运算符
    ostream & operator<<(ostream cout,Person &p){
    //本质 operator<<(cout,p) 简化 cout << p
        cout << p.m_A << p.m_B;
        return cout;
    }
    void test01(){
        Person p;
        p.m_A = 10;
        p.m_B = 20;
        cout << p << endl;//报错 无法直接输出p
        //当使用全局函数重载左移运算符后
        cout << p << endl;//可以输出了
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    3.递增运算符重载

    作用:通过重载递增运算符,实现自己定义整型数据的输出

    问题:

    int a = 10;
     a++;
     ++a;//可以直接输出
    class MyInteger{
        private:
        	int m_Num;
        public:
        	MyInteger(){
                m_Num = 0;
            }
    }
    MyInteger myint;
    //重载递增运算符 实现:
    myint++;
    ++myint;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    解决:

    class MyInteger{
        //定义operator<<为友元
        friend: ostream & operator<<(ostream cout,MyInteger &myint);
        private:
        	int m_Num;
        public:
        	MyInteger(){
                m_Num = 0;
            }
        //重载前置++运算符
        MyInteger& operator++(){
        //返回值为引用,为了一直对一个数据进行递增操作
            m_Num++;
            return *this;
        }
        //重载后置++运算符
         MyInteger operator++(int){
         //这个int 代表占位参数,可以用于区分前置++和后置++
            MyInteger temp = *this;
             m_Num++;
            return temp;
            //temp为局部变量,程序结束就会释放,所以返回值不能是引用。
        }
    }
    //利用全局函数重载左移运算符
    
    ostream & operator<<(ostream cout,MyInteger &myint){
    //本质 operator<<(cout,p) 简化 cout << p
    
        cout << myint.m_Num;
        //因为m_Num是私有属性,需要给该全局函数加上友元
        return cout;
    }
    void test01(){
        MyInteger myint;
        cout << myint <<endl;//报错,需要重载左移运算符
        cout << ++myint <<endl;//报错,当前运行自增运算需要重载递增运算符
        //在重新定义递增运算符后
        cout << ++myint <<endl;//可以运行
        cout << myint++ <<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    4.赋值运算符重载

    ​ C++编译器至少给一个类添加4个函数(原先是说3个函数)

    ​ 1.默认的构造函数(无参,函数体为空)

    ​ 2.默认的析构函数(无参,函数体为空)

    ​ 3.默认拷贝构造函数,对属性进行值拷贝。

    ​ 4.赋值运算符operator=,对属性进行值拷贝

    如果类中有属性指向堆区,做赋值操作也会出现深浅拷贝问题。

    class Person(){
        public:
        	int *m_Age;
        Person(int age){//把数据存放到堆区
            m_Age = new int(age);
        }
        ~Person(){
            //释放堆区数据
            if(m_Age!=null){
                delete m_Age;
                m_Age = null;
            }
         Person& operator=(Person &p){
                if(m_Age!=null){//先把p2堆区数据释放干净在进行赋值操作。
                    delete m_Age;
                    m_Age = null;
                }
                //深拷贝
                 m_Age = new int(*p.m_Age);
                return *this;
          }
        }
    }
    void test01(){
        Person p1(10);
        Person p2(18);
        Person p3(16);
        cout << "p1的年龄:"<<*p1.m_Age<<endl;//输出:p1的年龄:10
        cout << "p2的年龄:"<<*p2.m_Age<<endl;//输出:p0的年龄:18
        
        p2 = p1;/*赋值操作  如果不定义析构函数,进行堆区数据的释放就不会
        			进行报错,如果一旦定义了系统就会报错。报错原因和深浅拷
        			贝出错的原因一致,都是因为p1,p2都指向同一堆区数据,
        			其中p1或p2一旦有一方进行堆区数据的释放,另一方就会出错.
        			*/
        //解决方法:重载赋值运算符,让两者的数据分别写到堆区。
        //重写赋值操作后
        p2 = p1=p3;//正常运行
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    5.关系运算符重载

    ​ 作用:重载关系运算符,可以让两个自定义类型对象进行对比操作。

    //重载关系运算符
    class Person{
     public:
        int m_Age;
        string m_Name;
        Person(string name.int age){
            m_Name = name;
            m_Age = age;
        }
        bool operator==(Person &p){
            if(this.m_Name == p.m_Name &&this.m_Age == p.m_Age ){
                return true;
            }
            return false;
        }
    };
    void test01(){
        Person p1("tom",18);
        Person p2("tom",18);
        if(p1 == p2){//报错 p1==p2系统无法识别,需要重载,重载后就可以使用
            cout <<"p1和p2相等"<<endl;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    6.函数调用运算符重载

    ​ 1.函数调用运算符()也可以重载

    ​ 2.由于重载后使用方式非常想函数的调用,因此称为仿函数

    ​ 3.仿函数没有固定写法,非常灵活

    class MyPrint{
      	public:
        	//重载函数调用运算符
        	void operator()(string test){
                cout << test <<endl;
            }
    };
    //正常的函数
    void MyPrint02(string test){
            cout << test <<endl;
         }
    void test01(){
        MyPrint myPrint();
        //重载函数调用运算符,之后的调用
        myPrint("hello word");//由于使用起来非常类似于函数调用,
        						//因此称为仿函数
        //正常函数调用
        MyPrint02("hello word");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    Postgresql JIT README翻译
    C语言中的文件操作
    【IROS 2019】RangeNet++: 快速准确的LiDAR语义分割
    (31)Verilog实现单bit数据时钟域转换【快到慢】
    html+css+js网页设计 旅游 大理旅游7个页面
    Redis 由浅入深
    CV_8UC1,CV_8UC2,CV_8UC3
    四、伊森商城 前端基础-Vue 双向绑定&事件处理&安装插件 p22
    Three.js相机模拟
    Spring 声明式事务机制
  • 原文地址:https://blog.csdn.net/kaszxc/article/details/126928273