日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++超詳細講解操作符的重載_C 語言

作者:清風自在?流水潺潺 ? 更新時間: 2022-07-29 編程語言

一、需要解決的問題

下面的復數解決方案是否可行?

下面看一下復數的加法操作:

#include <stdio.h>
class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2)
{
    Complex ret;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = Add(c1, c2); // c1 + c2
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    return 0;
}

輸出結果如下:

思考

Add 函數可以解決 Complex 對象相加的問題,但是 Complex 是現實世界中確實存在的復數,并且復數在數學中的地位和普通的實數相同。

為什么不能讓+操作符也支持復數相加呢?這個就涉及到操作符的重載。

二、操作符重載

C++ 中的重載能夠擴展操作符的功能

操作符的重載以函數的方式進行

本質

用特殊形式的函數擴展操作符的功能

  • 通過 operator 關鍵字可以定義特殊的函數
  • operator 的本質是通過函數重載操作符

語法

下面來初探一下操作符重載:

#include <stdio.h>
class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
    Complex ret;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2; // operator + (c1, c2)
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    return 0;
}

輸出結果如下:

可以將操作符重載函數定義為類的成員函數

  • 比全局操作符重載函數少—個參數(左操作數)
  • 不需要依賴友元就可以完成操作符重載
  • 編譯器優先在成員函數中尋找操作符重載函數

下面來實現在成員函數中重載操作符:

#include <stdio.h>
class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    Complex operator + (const Complex& p)
    {
        Complex ret;
        printf("Complex operator + (const Complex& p)\n");
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;
        return ret;
    }
    friend Complex operator + (const Complex& p1, const Complex& p2);
};
Complex operator + (const Complex& p1, const Complex& p2)
{
    Complex ret;
    printf("Complex operator + (const Complex& p1, const Complex& p2)\n");
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2; // c1.operator + (c2)
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    return 0;
}

輸出結果如下:

這個說明編譯器優先在成員函數中尋找操作符重載函數

故上述代碼可以直接寫成:

#include <stdio.h>
class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
    Complex operator + (const Complex& p)
    {
        Complex ret;
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;
        return ret;
    }
};
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2; // c1.operator + (c2)
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    return 0;
}

三、小結

  • 操作符重載是 C++ 的強大特性之一
  • 操作符重載的本質是通過函數擴展操作符的功能
  • operator 關鍵字是實現操作符重載的關鍵
  • 操作符重載遵循相同的函數重載規則
  • 全局函數和成員函數都可以實現對操作符的重載

原文鏈接:https://blog.csdn.net/weixin_43129713/article/details/124362670

欄目分類
最近更新