最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++運算符重載的詳細講解

 更新時間:2021年04月07日 10:00:50   作者:Thrush''''s note  
這篇文章主要給大家介紹了關(guān)于C++運算符重載的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

加號運算符重載

對于內(nèi)置數(shù)據(jù)類型,編譯器知道如何運算

但是對于自己封裝的類,編譯器無法進行運算

這時可以通過自己定義運算符重載進行運算

operator+

通過成員函數(shù)重載+號

#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
 //通過成員函數(shù)實現(xiàn)重載
 Person operator+ (Person &p)
 {
 //創(chuàng)建一個臨時變量
 Person temp;
 temp.m_a = this->m_a + p.m_a;
 temp.m_b = this->m_b + p.m_b;
 return temp;
 }
};
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //通過函數(shù)原型調(diào)用
 p3 = p1.operator+(p2);
 //簡便調(diào)用
 //p3 = p1 + p2;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}

int main()
{
 test01();
 system("pause");
 return 0;
}

注意兩種調(diào)用方式

通過函數(shù)原型調(diào)用

p3 = p1.operator+(p2);

簡便調(diào)用

p3 = p1 + p2;

通過全局函數(shù)重載+號

#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
};
//通過全局函數(shù)實現(xiàn)重載
Person operator+ (Person& p1, Person& p2)
{
 //創(chuàng)建一個臨時變量
 Person temp;
 temp.m_a = p1.m_a + p2.m_a;
 temp.m_b = p1.m_b + p2.m_b;
 return temp;
}
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //函數(shù)原型調(diào)用
 p3 = operator+(p1,p2);
 //簡便調(diào)用
 //p3 = p1 + p2;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

注意兩種調(diào)用方式

通過函數(shù)原型調(diào)用

p3 = operator+(p1,p2);

簡便調(diào)用

p3 = p1 + p2;

運算符重載發(fā)生函數(shù)重載

運算符重載可以發(fā)生函數(shù)重載:Person+int等等

#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
};
//通過全局函數(shù)實現(xiàn)重載
Person operator+ (Person& p1, int num)
{
 //創(chuàng)建一個臨時變量
 Person temp;
 temp.m_a = p1.m_a + num;
 temp.m_b = p1.m_b + num;
 return temp;
}
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //函數(shù)原型調(diào)用
 //p3 = operator+(p1,55);
 //簡便調(diào)用
 p3 = p1 + 55;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

調(diào)用方法和定義方法與上面相同,不再多余贅述

總結(jié)

1、系統(tǒng)內(nèi)置數(shù)據(jù)類型的表達式不可改變

2、不要濫用運算符重載

左移運算符

不利用成員函數(shù)重載左移運算符

沒有具體演示,因為報錯,我也沒寫出來

下面通過全局函數(shù)實現(xiàn)

#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
 
};
ostream& operator<<(ostream& cout,Person&p)
{
 cout << "p.m_a=" <<p. m_a << " p.m_b=" <<p. m_b << endl;
 return cout;
}
void test01()
{
 Person p1;
 p1.m_a = 44;
 p1.m_b = 66;
 cout << p1 << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

因為要實現(xiàn)鏈式,實現(xiàn)追加,所以返回值必須是ostream

總結(jié)

配合友元實現(xiàn)自定義輸出類型

遞增運算符重載

遞增運算符重載

#include<iostream>
using namespace std;
class myInt
{
 friend ostream& operator<<(ostream& cout, myInt num);
public:
 myInt()
 {
 this->m_a = 0;
 }
 //前置++運算符重載
 myInt& operator++()//返回引用是為了一直對一個數(shù)據(jù)進行遞增,否則函數(shù)默認返回一個新的數(shù)
 {
 //先進行++
 m_a++;
 //然后返回自身
 return *this;
 }
 //后置++運算符重載
 myInt operator++(int)//int表示占位參數(shù),用于區(qū)分前置后置參數(shù)
 {
 //先記錄當前的值
 myInt temp=*this;
 //再遞增
 m_a++;
 //然后返回記錄的值
 return temp;
 }
private:
 int m_a; 
};
//左移運算符重載
ostream& operator<<(ostream& cout, myInt num)
{
 cout << num.m_a;
 return cout;
}
void test01()
{
 myInt myint;
 cout << myint << endl;
 cout << ++myint << endl;
 cout << myint << endl;

}
int main()
{
 test01();
 system("pause");
 return 0;
}

賦值運算符重載

#include<iostream>
using namespace std;
class Person
{
public:
 Person(int num)//將數(shù)據(jù)開辟到堆區(qū)
 {
 m_a = new int(num);
 }
 ~Person()
 {
 if (m_a != NULL)
 {
  delete m_a;
  m_a = NULL;
 }
 }
 //重載賦值運算符
 Person& operator=(Person &p)//返回值用Person返回本身,可執(zhí)行連等
 {
 //先判斷是否有屬性在堆區(qū),如果有先釋放干凈
 if (m_a != NULL)
 {
  delete m_a;
  m_a = NULL;
 }
 m_a = new int(*p.m_a);
 return *this;
 }
 int* m_a;
 
};
void test01()
{
 Person p1(18);
 Person p2(209);
 Person p3(9);
 p2 = p1 = p3;
 cout << *p1.m_a << endl;
 cout << *p2.m_a << endl;
 cout << *p3.m_a << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

關(guān)系運算符重載

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:
 
 Person(string name, int age)
 {
 this->age = age;
 this->name = name;
 }
 bool operator==(Person& p)
 {
 if (this->age == p.age && this->name == p.name)
 {
  return true;
 }
 else {
  return false;
 }
 }
 bool operator!=(Person& p)
 {
 if (this->age == p.age && this->name == p.name)
 {
  return false;
 }
 else {
  return true;
 }
 }
 string name;
 int age;

 
};
void test01()
{
 Person p1("gouride", 19);
 Person p2("gouride", 19);
 if (p1 == p2) {
 cout << "p1和p2相同" << endl;
 }
 else {
 cout << "p1和p2不相同" << endl;
 }
 if (p1 != p2) {
 cout << "p1和p2不相同" << endl;
 }
 else {
 cout << "p1和p2相同" << endl;
 }
}
int main()
{
 test01();
 system("pause");
 return 0;
}

函數(shù)調(diào)用重載

仿函數(shù)

#include<iostream>
using namespace std;
#include<string>
class Myprint
{
public:
 void operator()(string name)
 {
 cout << name << endl;
 }
 int operator()(int a,int b)
 {
 return a + b;
 }
};
void test01()
{
 
 Myprint myprint;
 myprint("測試");
 //匿名對象調(diào)用
 cout << Myprint()(4,6) << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

總結(jié)

到此這篇關(guān)于C++運算符重載的文章就介紹到這了,更多相關(guān)C++運算符重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++Stack棧類模版實例詳解

    C++Stack棧類模版實例詳解

    這篇文章主要為大家詳細介紹了C++Stack棧類模版實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • 關(guān)于函數(shù)調(diào)用方式__stdcall和__cdecl詳解

    關(guān)于函數(shù)調(diào)用方式__stdcall和__cdecl詳解

    下面小編就為大家?guī)硪黄P(guān)于函數(shù)調(diào)用方式__stdcall和__cdecl詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • C語言 運算符詳細介紹及示例代碼

    C語言 運算符詳細介紹及示例代碼

    本文介紹C語言 運算符,這里整理了運算符的基礎(chǔ)知識,并附示例代碼,希望能幫助剛剛開始學(xué)習(xí) C語言的同學(xué)
    2016-08-08
  • 如何判斷一個整數(shù)的二進制中有多少個1

    如何判斷一個整數(shù)的二進制中有多少個1

    本篇文章是對如何判斷一個整數(shù)的二進制中有多少個1的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 淺析C/C++中被人誤解的SIZEOF

    淺析C/C++中被人誤解的SIZEOF

    以下是對C/C++中的SIZEOF進行了詳細的分析介紹,需要的朋友參考下
    2013-07-07
  • C++線程池實現(xiàn)代碼

    C++線程池實現(xiàn)代碼

    C++11中,線程我們可以理解為對應(yīng)一個thread對象,任務(wù)可以理解為要執(zhí)行的函數(shù),通常是耗時的函數(shù)。線程過多或者頻繁創(chuàng)建和銷毀線程會帶來調(diào)度開銷,進而影響緩存局部性和整體性能
    2021-12-12
  • C++標準庫bitset類型的簡單使用方法介紹

    C++標準庫bitset類型的簡單使用方法介紹

    這篇文章主要介紹了C++標準庫bitset類型的簡單使用方法,需要的朋友可以參考下
    2017-07-07
  • Qt重寫QTreeView自繪實現(xiàn)酷炫樣式

    Qt重寫QTreeView自繪實現(xiàn)酷炫樣式

    QTreeView,顧名思義,就是一種樹形的控件,在我們需要做類似于文件列表的視圖時,是一個不錯的選擇,下面我們就來看看qt如何重寫QTreeView實現(xiàn)酷炫樣式,感興趣的可以了解一下
    2023-08-08
  • Visual Studio安裝的圖文教程

    Visual Studio安裝的圖文教程

    這篇文章主要介紹了Visual Studio安裝的圖文教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C++使用BitBlt進行窗口抓圖的方法

    C++使用BitBlt進行窗口抓圖的方法

    這篇文章主要介紹了C++使用BitBlt進行窗口抓圖的方法,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下
    2021-01-01

最新評論

正定县| 西昌市| 农安县| 弥勒县| 龙江县| 那坡县| 泾阳县| 大悟县| 宜阳县| 海原县| 南木林县| 界首市| 洛隆县| 博野县| 大英县| 闸北区| 阿拉善盟| 柳州市| 钦州市| 青州市| 郎溪县| 扬中市| 临澧县| 临湘市| 黄大仙区| 和顺县| 荆门市| 武宁县| 怀柔区| 师宗县| 灵台县| 康乐县| 广汉市| 崇左市| 泗阳县| 龙山县| 寻甸| 永顺县| 丹巴县| 达孜县| 合水县|