運算符重載注意
- 重載的運算符要易讀
- 內置的數據類型的表達式的運算符是不可以改變的
- 不要重載
&&
和| |
運算符 =
,[]
和->
運算符只能通過成員函數進行重載<<
和>>
只能通過全局函數配合友元函數進行重載
加號運算符重載
-
如果想讓自定義數據類型 進行**+**運算,那么就需要重載 **+**運算符
-
在成員函數 或者 全局函數里 重寫 一個**+**運算符的函數
-
函數名operate+(){}
-
運算符重載可以不停重載,接著重載
#include<iostream>using namespace std;class Person{public:Person(){}Person(int a, int b) :m_A(a), m_B(b){}//+號運算符重載 成員函數/*Person operator+(Person & p){Person tmp;tmp.m_A= this->m_A + p.m_A;tmp.m_B = this->m_B + p.m_B;return tmp;}*/int m_A;int m_B;};//利用全局函數,進行+號運算符的重載Person operator+(Person &p1, Person &p2) //二元{Person tmp;tmp.m_A = p1.m_A + p2.m_A;tmp.m_B = p1.m_B + p2.m_B;return tmp;}//函數重載 附帶運算符再次重載Person operator+(Person &p1, int a) //二元{Person tmp;tmp.m_A = p1.m_A + a;tmp.m_B = p1.m_B + a;return tmp;}void test01(){Person p1(10, 10);Person p2(10, 10);Person p3 = p1 + p2; //p1+p2 從什么表達式轉變的? p1.operator(p2) operator(p1,p2)Person p4 = p1 + 10;//重載的版本cout << "p3的m_A" << p3.m_A << endl << "p3的m_B" << p3.m_B << endl;}int main(){test01();system("pause");return 0;}
左移運算符重載
-
不要隨意亂用符號重載
-
內置數據類型的運算符不可以重載
-
cout<< 直接對Person自定義數據類型 進行輸出
-
寫到全局函數中 ostream& operator<<(ostream & cout,Person & p1){}
-
如果重載時想訪問p1的私有成員,那么全局函數要做Person的友元函數
#include<iostream>using namespace std;class Person{friend ostream & operator <<(ostream &cout, Person&p1);public:Person(){}Person(int a, int b){this->m_A = a;this->m_B = b;}/*void operator<<() 重載左移運算符不可以寫到成員函數中{}*/private:int m_A;int m_B;};//cout是屬于ostream類 //如果向訪問私有成員,就要用友元函數ostream & operator <<(ostream &cout, Person&p1) //第一個參數cout 第二個參數p1{cout << "m_A=" << p1.m_A << "m_B=" << p1.m_B;return cout;}void test01(){Person p1(10, 10);cout << p1<<endl;}int main(){test01();system("pause");return 0;}
前置,后置 ++運算符重載
-
自己實現
int
類型MyIntege
-
內部維護
int
數據 -
MyInteger myint
-
myint ++ 后置 ++myint前置
-
重載++運算符 operator++()前置 operator++(int )后置
-
前置理念 先++ 后返回自身 后置理念 先保存原有值 內部++ 返回臨時數據
#include<iostream>using namespace std;class MyInteger{friend ostream & operator<<(ostream &cout, MyInteger& myInt);public:MyInteger(){m_Num = 0;}//前置++重載//返回引用MyInteger & operator++(){this->m_Num++;return *this;}//用占位參數區分前置和后置//后置++重載//返回值,MyInteger operator++(int){//先保存目前的數據MyInteger tmp = *this;//實際數據++m_Num++;//返回++前的數值return tmp;}//所以前置++好,因為返回引用,少一份開銷int m_Num;};ostream & operator<<(ostream &cout, MyInteger& myInt){cout << myInt.m_Num;return cout;}void test01(){MyInteger myInt;//前置++值cout << ++myInt << endl;//后置++值cout << myInt++ << endl;//本身的值cout << myInt << endl;}int main(){test01();system("pause");return 0;}
賦值運算符重載
-
系統默認給類提供 賦值運算符寫法 是簡單值拷貝
-
導致如果類中有指向堆區的指針,就可能出現深淺拷貝的問題
-
所以要重載=運算符
-
鏈式編程return *this
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class Person{public:Person(int a){this->m_A = a;}int m_A;};void test01(){Person p1(10);Person p2(0);p2 = p1;cout << "p2的m_A" << p2.m_A << endl;}class Person2{public:Person2(char * name){this->pName = new char[strlen(name) + 1];strcpy(this->pName, name);}//類中重載 =賦值運算符Person2 & operator=(const Person2 &p){//判斷如果原來已經堆區有內容,先釋放if (this->pName != NULL){delete[]this->pName;this->pName = NULL;}this->pName = new char[strlen(p.pName) + 1];strcpy(this->pName, p.pName);return *this;}~Person2(){if (this->pName != NULL){delete[]this->pName;this->pName = NULL;}}char * pName;};void test02(){Person2 p1("狗蛋");Person2 p2("狗剩");Person2 p3("");p3=p2 = p1;cout << p2.pName << endl;cout << p3.pName << endl;cout << p2.pName << endl;int a = 10;int b = 20;int c;c = a = b; //都是20cout << a << " " << b << " " << c << endl;}int main(){test02();system("pause");return 0;}
關系運算符重載
#include<iostream>#include<string>using namespace std;class Person{public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(Person &p){if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age){return true;}return false;}bool operator!=(Person &p){if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age){return false;}return true;}string m_Name;int m_Age;};void test01(){Person p1("小明", 10);Person p2("小強", 15);Person p3("小強", 15);/*int a = 10;int b = 10;if (a == b){cout << "a,b相等" << endl;}*/if (p1 == p2){cout << "p1和p2相等" << endl;}else{cout << "不相等" << endl;}if (p3 == p2){cout << "p3和p2相等" << endl;}else{cout << "不相等" << endl;}if (p1 != p2){cout << "p1和p2不相等" << endl;}else{cout << "相等" << endl;}}int main(){test01();system("pause");return 0;}
函數調用運算符重載
#include<iostream>#include<string>using namespace std;// ()重載class MyPrint{public:void operator()(string text){cout << text << endl;}};void test01(){MyPrint myPrint;myPrint("hello world"); //仿函數}class MyAdd{public:int operator()(int v1,int v2){return v1 + v2;}};void test02(){/*MyAdd myAdd;cout << myAdd(1, 1) << endl;*/cout << MyAdd()(1, 1) << endl; //匿名對象}int main(){//test01();test02();system("pause");return 0;}