• 运算符重载(一)


    什么是运算符重载?

    运算符重载就是把标准运算符(例如=,-,+,>等)应用于我们自己定义的数据类型的对象。其实重载本身的含义就是一次多意。函数重载就是一个函数名有多种不同的含义。

    运算符重载的好处是直观自然,可以提高程序的可读性 体现了C++的可扩充性 运算符重载仅仅只是语法上的方便,它是另一种函数调用的方式 运算符重载,本质上是函数重载 不要滥用重载、因为它只是语法上的方便,所以只有在涉及的代码更容易写、尤其是更易读时才有必要重载。

    运算符重载的形式有两种一种是成员函数形式的运算符重载,一种是非成员函数的形式的运算符重载

    成员函数原型的格式: 函数类型 operator 运算符(参数表); 成员函数定义的格式: 函数类型 类名::operator 运算符(参数表){ 函数体; }

    友元函数原型的格式: friend 函数类型 operator 运算符(参数表); 友元函数定义的格式: friend 函数类型 类名::operator 运算符(参数表){ 函数体; }

    (一)成员函数形式重载举例:

     1 //Complex.h
     2 #ifndef COMPLEX_H
     3 #define COMPLEX_H
     4 class Complex
     5 {
     6 public:
     7     Complex();
     8     Complex(int real,int imag);
     9     ~Complex();
    10     Complex& Add(const Complex& other);
    11     void Display()const;
    12     Complex operator+(const Complex& other);//运算符重载函数声明
    13 private:
    14     int m_real;
    15     int m_imag;
    16 };
    17 #endif //COMPLEX_H
     1 #include "Complex.h"
     2 #include<iostream>
     3 using std::endl;
     4 using std::cout;
     5 
     6 Complex::Complex()
     7 {
     8 }
     9 Complex::Complex(int real, int imag) :m_imag(imag), m_real(real)
    10 {
    11 
    12 }
    13 
    14 
    15 Complex::~Complex()
    16 {
    17 }
    18 
    19 Complex& Complex::Add(const Complex& other){
    20     m_real += other.m_real;
    21     m_imag += other.m_imag;
    22     return *this;
    23 }
    24 Complex Complex::operator+(const Complex& other){//运算符重载定义
    25     int r = m_real + other.m_real;
    26     int i = m_imag + other.m_imag;
    27     return Complex(r, i);
    28 }
    29 void Complex::Display()const{
    30 
    31     cout << "m_real=" << m_real << endl;
    32     cout << "m_imag=" << m_imag << endl;
    33 }
     1 #include<iostream>
     2 #include"Complex.h"
     3 using std::endl;
     4 using std::cout;
     5 int main(){
     6     Complex c1(2,1);
     7     Complex c2(7,3);
     8     Complex c3 = c1 + c2;
     9     c1.Display();
    10     c2.Display();
    11     c3.Display();
    12     return 0;
    13 }

    (二)友元函数重载运算符举例:

    #ifndef COMPLEX_H
    #define COMPLEX_H
    class Complex
    {
    public:
        Complex();
        Complex(int real,int imag);
        ~Complex();
        Complex& Add(const Complex& other);
        void Display()const;
        //Complex operator+(const Complex& other);
        friend Complex operator+(const Complex& left,const Complex& right);//友元函数声明
    private:
        int m_real;
        int m_imag;
    };
    #endif //COMPLEX_H
    1 Complex operator+(const Complex& left, const Complex& right){
    2     int r = left.m_real + right.m_real;
    3     int i = left.m_imag + right.m_imag;
    4     return Complex(r,i);
    5 }

    此处有一个注意的小点,就是在重载+号运算符时,可以成员函数重载,友元函数重载一起使用,但是设置断点后跟踪,发现调用的是成员函数重载的运算符。说明成员函数重载运算符比友元函数重载的优先级要高。

    运算符重载的规则:

    运算符重载不允许发明新的运算符。

    不能改变运算符操作对象的个数。

    运算符被重载后,其优先级和结合性不会改变。

    一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。

    以下一些双目运算符不能重载为类的友元函数:=、()、[]、->。

    类型转换运算符只能以成员函数方式重载

    流运算符只能以友元的方式重载

  • 相关阅读:
    socket.io
    CUDA升级后
    QT安装
    windows时钟服务设置
    QT的DPI支持
    cudaDeviceProp结构体
    C#调用C++的dll各种传参
    「LibreOJ#516」DP 一般看规律
    「LibreOJ#515」贪心只能过样例 (暴力+bitset)
    [Codeforces888E]Maximum Subsequence(暴力+meet-in-the-middle)
  • 原文地址:https://www.cnblogs.com/cplinux/p/5624008.html
Copyright © 2020-2023  润新知