c++中运算符重载(加号运算,左移运算,前置后置++运算符,赋值运算,关系运算,函数运算)
運(yùn)算符重載注意
加號(hào)運(yùn)算符重載
如果想讓自定義數(shù)據(jù)類型 進(jìn)行**+**運(yùn)算,那么就需要重載 **+**運(yùn)算符
在成員函數(shù) 或者 全局函數(shù)里 重寫 一個(gè)**+**運(yùn)算符的函數(shù)
函數(shù)名operate+(){}
運(yùn)算符重載可以不停重載,接著重載
#include<iostream>using namespace std;class Person{public:Person(){}Person(int a, int b) :m_A(a), m_B(b){}//+號(hào)運(yùn)算符重載 成員函數(shù)/*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;};//利用全局函數(shù),進(jìn)行+號(hào)運(yùn)算符的重載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;}//函數(shù)重載 附帶運(yùn)算符再次重載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 從什么表達(dá)式轉(zhuǎn)變的? 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;}左移運(yùn)算符重載
不要隨意亂用符號(hào)重載
內(nèi)置數(shù)據(jù)類型的運(yùn)算符不可以重載
cout<< 直接對(duì)Person自定義數(shù)據(jù)類型 進(jìn)行輸出
寫到全局函數(shù)中 ostream& operator<<(ostream & cout,Person & p1){}
如果重載時(shí)想訪問p1的私有成員,那么全局函數(shù)要做Person的友元函數(shù)
#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<<() 重載左移運(yùn)算符不可以寫到成員函數(shù)中{}*/private:int m_A;int m_B;};//cout是屬于ostream類 //如果向訪問私有成員,就要用友元函數(shù)ostream & operator <<(ostream &cout, Person&p1) //第一個(gè)參數(shù)cout 第二個(gè)參數(shù)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;}前置,后置 ++運(yùn)算符重載
自己實(shí)現(xiàn)int類型MyIntege
內(nèi)部維護(hù)int 數(shù)據(jù)
MyInteger myint
myint ++ 后置 ++myint前置
重載++運(yùn)算符 operator++()前置 operator++(int )后置
前置理念 先++ 后返回自身 后置理念 先保存原有值 內(nèi)部++ 返回臨時(shí)數(shù)據(jù)
#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;}//用占位參數(shù)區(qū)分前置和后置//后置++重載//返回值,MyInteger operator++(int){//先保存目前的數(shù)據(jù)MyInteger tmp = *this;//實(shí)際數(shù)據(jù)++m_Num++;//返回++前的數(shù)值return tmp;}//所以前置++好,因?yàn)榉祷匾?#xff0c;少一份開銷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;}賦值運(yùn)算符重載
系統(tǒng)默認(rèn)給類提供 賦值運(yùn)算符寫法 是簡(jiǎn)單值拷貝
導(dǎo)致如果類中有指向堆區(qū)的指針,就可能出現(xiàn)深淺拷貝的問題
所以要重載=運(yùn)算符
鏈?zhǔn)骄幊蘲eturn *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);}//類中重載 =賦值運(yùn)算符Person2 & operator=(const Person2 &p){//判斷如果原來(lái)已經(jīng)堆區(qū)有內(nèi)容,先釋放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;}關(guān)系運(yùn)算符重載
#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("小強(qiáng)", 15);Person p3("小強(qiáng)", 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;}函數(shù)調(diào)用運(yùn)算符重載
#include<iostream>#include<string>using namespace std;// ()重載class MyPrint{public:void operator()(string text){cout << text << endl;}};void test01(){MyPrint myPrint;myPrint("hello world"); //仿函數(shù)}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; //匿名對(duì)象}int main(){//test01();test02();system("pause");return 0;}總結(jié)
以上是生活随笔為你收集整理的c++中运算符重载(加号运算,左移运算,前置后置++运算符,赋值运算,关系运算,函数运算)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF战斗法师
- 下一篇: c++实现字符串类的封装