運(yùn)算符重載

+、-、*、/、++、--、==、!=、* ->、&&、||...

對(duì)于內(nèi)置數(shù)據(jù)類型,編譯器知道如何做運(yùn)算,編譯器不知道如何讓兩個(gè)類進(jìn)行運(yùn)算

  1. 如果向讓自定義數(shù)據(jù)類型 進(jìn)行+法運(yùn)算,就需要重載+運(yùn)算符
  2. 在成員函數(shù)或者全局函數(shù)里 ,重寫一個(gè)+法運(yùn)算符的函數(shù)
  3. 函數(shù)名 operator+(){}
  4. 運(yùn)算符重載也可以提供多個(gè)版本

加法運(yùn)算符

類名+operator+(){};

例:

成員函數(shù)

class Person
{
public:
    Person()
    {}
    Person(int a, int b) :a(a), b(b)
    {}
    Person operator+(Person& p)
    {
        Person tmp;
        tmp.a = this->a + p.a;
        tmp.b = this->b + p.b;
        return tmp;
    }
    int a;
    int b;
};
void test()
{
    Person p1(1, 20);
    Person p2(3, 34);
    Person p3 = p2 + p1;
    cout << p3.a << "  " << p3.b << endl;
}

例:

全局函數(shù)

class Person
{
public:
    Person()
    {}
    Person(int a, int b) :a(a), b(b)
    {}

    int a;
    int b;
};
    Person operator+(Person &p1,Person& p2)
    {
        Person tmp;
        tmp.a = p1.a + p2.a;
        tmp.b = p1.b + p2.b;
        return tmp;
    }
void test()
{
    Person p1(1, 20);
    Person p2(3, 34);
    Person p3 = p2 + p1;
    cout << p3.a << "  " << p3.b << endl;
}

左移運(yùn)算符重載

  • 不要隨意亂用符號(hào)重載
  • 內(nèi)置數(shù)據(jù)類型的運(yùn)算符不可以重載
  • cou<<直接對(duì)Person自定義數(shù)據(jù)類型 進(jìn)行輸出
  • 寫到全局函數(shù)中 ostream& operator<<(ostream& cout,Person &p){}
  • 如果重載時(shí)候想訪問p的私有成員,那么全局函數(shù)要做person的友元函數(shù)

例:

class Person
{
//友元函數(shù)
    friend ostream& operator<<(ostream& cout, Person& p);
public:
    Person()
    {}
    Person(int a, int b) :a(a), b(b)
    {}
private:
    int a;
    int b;
};

//<<重載
ostream& operator<<(ostream& cout, Person& p)
{
    cout << "a=" << p.a << "b=" << p.b;
    return cout;
}
void test()
{
    Person p(23, 54);
    cout << p << endl;
}

++運(yùn)算符重載

重載++運(yùn)算符 operator++()前置 operator++(int)后置

前置理念:先++,后返回自身

后置理念:先保存住原值,內(nèi)部++,返回臨時(shí)數(shù)據(jù)

例:

class MyInteter
{
    
    friend ostream& operator<<(ostream &cout, MyInteter myInteter);
    
public:
    MyInteter()
    {
        num = 0;
    };
    //前置++重載
    MyInteter& operator++()
    {
        this->num++;
        return *this;
    };
    //后置++重載  tmp是臨時(shí)數(shù)據(jù),數(shù)據(jù)結(jié)束會(huì)銷毀,所以要返回值
    MyInteter operator++(int)
    {
        MyInteter tmp=*this;
        num++;
        return tmp;
    }
private:
    int num;
};
//此處參數(shù)如果是引用,后置++返回的數(shù)據(jù)已經(jīng)銷毀了,會(huì)導(dǎo)致錯(cuò)誤,因此要傳值
ostream& operator<<(ostream& cout, MyInteter myInt)
{
    cout << myInt.num;
    return cout;
};
void test()
{
    MyInteter my;

    cout << ++my << endl;
    cout <<my++<< endl;
    cout << my << endl;
}

指針運(yùn)算符重載(智能指針)

用來托管自定義類型的對(duì)象,讓對(duì)象進(jìn)行自動(dòng)的釋放。

有了智能指針,讓智能指針托管這個(gè)person對(duì)象,對(duì)象的釋放就不用操心了,讓智能指針管理。

為了讓智能指針向普通的Person*指針一樣使用,就要重寫->和*

例:

class Person
{
public:
    Person()
    {

    }
    Person(int age)
    {
        this->age = age;
    }
    ~Person()
    {
        cout << "Person析構(gòu)了" << endl;
    }
    void showAge()
    {
        cout << "年齡為:" << this->age << endl;
    }
    int age;
};

class smartPointer
{
public:
    smartPointer()
    {

    }
    smartPointer(Person* p)
    {
        this->person = p;
    }
    //重載指針
    Person* operator->()
    {
        return this->person;
    }
    //重載*
    Person& operator*()
    {
        return *person;
    }
    ~smartPointer()
    {
        cout << "智能指針析構(gòu)了" << endl;
        if (this->person)
        {
            delete this->person;
            this->person = NULL;
        }
    }
private:
    Person* person;
};

void test()
{
    smartPointer sn(new Person(10));
    sn->showAge();
     (*sn).showAge();
}

賦值運(yùn)算符重載

系統(tǒng)默認(rèn)給類提供的賦值運(yùn)算符寫法是簡單值拷貝,所以要重載=運(yùn)算符

如果想鏈?zhǔn)骄幊?,就要重?運(yùn)算符

class Person
{
public:
    Person()
    {

    }
    Person(char* name)
    {//開辟新空間
        this->name =new char[strlen(name)+1];
        //拷貝數(shù)據(jù)
        strcpy(this->name, name);
    }
    ~Person()
    {
        //判斷是否為空,不為空釋放
        if (this->name)
        {
            delete[] this->name;
            this->name = NULL;
        }
    }
    //重載=號(hào)運(yùn)算符
    Person& operator=(Person &p)
    {
    //判斷是否為空,不為空釋放
        if (this->name)
        {
            delete[] this->name;
            this->name = NULL;
        }
        //開辟新空間
        this->name = new char[strlen(p.name) + 1];
        拷貝數(shù)據(jù)
        strcpy(this->name,p.name);
        return *this;
    }
    char* name;
};



void test()
{
    
    Person p1((char*)"小紅");
    
    Person p2((char*)"小花");
    p1 = p2;
    cout << p1.name << endl;
}

[]運(yùn)算符重載

返回?cái)?shù)組索引的引用

int& operator[](int index)
pAddress是數(shù)組指針
return this->pAddress[index]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基本上我們進(jìn)行運(yùn)算符重載時(shí)有兩種形式,類內(nèi)的運(yùn)算符重載和頂層函數(shù)位置的運(yùn)算符重載。 操作符重載指的是將C++提供的...
    飛揚(yáng)code閱讀 1,784評(píng)論 0 4
  • 運(yùn)算符重載 當(dāng)運(yùn)算符作用域類類型對(duì)象時(shí),可以通過運(yùn)算符重載重新定義該運(yùn)算符的含義,明智使用運(yùn)算符重載能令我們的代碼...
    土豆吞噬者閱讀 2,659評(píng)論 0 0
  • C++語言的一個(gè)很有意思的特性就是除了支持函數(shù)重載外還支持運(yùn)算符重載,原因就是在C++看來運(yùn)算符也算是一種函數(shù)。比...
    歐陽大哥2013閱讀 2,791評(píng)論 0 8
  • 運(yùn)算符重載(Operator Overloading) 操作符重載的要點(diǎn) 操作符的通用語法 雙目操作符:<左操作數(shù)...
    DBreak閱讀 1,762評(píng)論 0 1
  • 一、 運(yùn)算符重載 1. 什么是運(yùn)算符重載 重載這個(gè)概念在早前的函數(shù)重載,大家已經(jīng)見識(shí)過了。函數(shù)可以重載, 運(yùn)算符也...
    送分童子笑嘻嘻閱讀 4,593評(píng)論 0 0

友情鏈接更多精彩內(nèi)容