C++基础11-类和对象之操作符重载2
生活随笔
收集整理的這篇文章主要介紹了
C++基础11-类和对象之操作符重载2
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
總結(jié):
1、等號(hào)操作符重載和拷貝構(gòu)造函數(shù)重載一般用在數(shù)據(jù)成員中需要單獨(dú)在堆區(qū)開(kāi)辟內(nèi)存時(shí)(指針)
2、new,delete重載內(nèi)部還是使用malloc和free
3、逗號(hào)表達(dá)式(,)、或者(||),且(&&),條件表達(dá)式(?:)具有短路功能。
????? 但重載后失去此功能,故不建議重載這兩個(gè)運(yùn)算符
4、自定義智能指針auto_ptr(在c++11新特性中已經(jīng)被移除)是一個(gè)模板類。
? ? ? 可以自動(dòng)被回收,自動(dòng)釋放
等號(hào)操作符重載:
?
?new和delete操作符重載:
#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class A { public:A(){cout << "A()..." << endl;}A(int a) {cout << "A(int)..." << endl;this->a = a;}//重載的new操作符 依然會(huì)觸發(fā)對(duì)象的構(gòu)造函數(shù)void* operator new(size_t size) {cout << "重載new操作符" << endl;return malloc(size);}void* operator new[](size_t size) {cout << "重載new[]操作符" << endl;return malloc(size);}void operator delete(void *p) {cout << "重載了delete操作符" << endl;if (p != NULL) {free(p);p = NULL;}}void operator delete[](void *p) {cout << "重載了delete[]操作符" << endl;if (p != NULL) {free(p);p = NULL;}}~A() {cout << "~A().... " << endl;}private:int a; }; void test01() {int *value_p = new int;A *ap = new A(10);//等價(jià)于//ap->operator new(sizeof(A));delete ap; } /* 重載new操作符 A(int)... ~A().... 重載了delete操作符 */ void test02() {int *array = (int *)malloc(sizeof(int) * 80);A *array_p = new A[5];//等價(jià)于//array_p->operator new[](sizeof(A[5]));delete[] array_p; } /* 重載new[]操作符 A()... A()... A()... A()... A()... ~A().... ~A().... ~A().... ~A().... ~A().... 重載了delete[]操作符*/ int main(void) {test01();cout << "test02" << endl;test02();return 0; } #endif&&和||操作符重載(不建議使用,使用了短路功能):
#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class Test { public:Test(int value) {this->value = value;}Test operator+(Test &another){cout << "執(zhí)行了+操作符重載" << endl;Test temp(this->value + another.value);return temp;}bool operator&&(Test &another){cout << "執(zhí)行了&&操作符重載" << endl;if (this->value && another.value) {return true;}else {return false;}}bool operator||(Test &another){cout << "重載了||操作符" << endl;if (this->value || another.value) {return true;}else {return false;}}~Test() {cout << "~Test()..." << endl;} private:int value; }; void test01() {Test t1(0);Test t2(20);//重載&&操作符,并不會(huì)發(fā)生短路現(xiàn)象。if (t1 && t2) { //t1.operator&&(t2)cout << "為真" << endl;}else {cout << "為假" << endl;} } /* 執(zhí)行了&&操作符重載 為假 ~Test()... ~Test()... */ void test02() {int a = 0;int b = 20;if (a && (a = 10)) { //a為0的話,a+b 就不會(huì)執(zhí)行 短路cout << "true" << endl;}cout << "a:" << a << endl;Test t1(0);Test t2(20);//重載&&操作符,并不會(huì)發(fā)生短路現(xiàn)象。if (t1 && (t1 + t2)) { //t1.operator&&(t1.operator+(t2))cout << "為真" << endl;}else {cout << "為假" << endl;} } /* a:0 執(zhí)行了+操作符重載 ~Test()... 執(zhí)行了&&操作符重載 ~Test()... 為假 ~Test()... ~Test()... */ void test03() {Test t1(0);Test t2(20);//重載||操作符,并不會(huì)發(fā)生短路現(xiàn)象if (t1 || (t1 + t2)) {//t1.operator||( t1.operator+(t2) )cout << "為真" << endl;}else {cout << "為假" << endl;} } /* 執(zhí)行了+操作符重載 ~Test()... 重載了||操作符 ~Test()... 為真 ~Test()... ~Test()... */ int main(void) {int a = 1;int b = 20;test01();cout << "test02" << endl;test02();cout << "test03" << endl;test03();return 0; } #endif自定義智能指針(stl中模板類,指針用完自動(dòng)回收,不需要手動(dòng)delete):
#if 1 #include<iostream> using namespace std; #include<memory> //智能指針是自動(dòng)被回收,自動(dòng)釋放 //只能指針是一個(gè)模板類 class A { public:A(int a){cout << "A()..." << endl;this->a = a;}void func() {cout << "a = " << this->a << endl;}~A() {cout << "~A()..." << endl;} private:int a; };void test01() {int *p = new int;*p = 20;delete p; } void test02() {//int *p = new int;//等價(jià)于auto_ptr<int> ptr(new int);*ptr = 100; } void test03() { #if 0A* ap = new A(10);ap->func();(*ap).func();delete ap; #endif//等價(jià)于auto_ptr<A> ap(new A(10));ap->func();(*ap).func(); //智能指針用完后 自動(dòng)回收 調(diào)用析構(gòu)函數(shù) } /* A()... a = 10 a = 10 ~A()... */ class MyAutoPtr { public:MyAutoPtr(A* ptr) {this->ptr = ptr; //ptr=new A(10)}~MyAutoPtr() {if (this->ptr != NULL) {cout << "delete ptr" << endl;delete ptr;ptr = NULL;}}A* operator->() {return this->ptr;}A& operator*(){return *ptr; //*ptr表示返回對(duì)象本身} private:A *ptr; //內(nèi)部指針 }; void test04() {MyAutoPtr myp(new A(10));myp->func(); //myp.ptr->func()(*myp).func();//*ptr.func() } /* A()... delete ptr ~A()... */ int main(void) {//test02();//test03();test04();return 0; } #endif?
總結(jié)
以上是生活随笔為你收集整理的C++基础11-类和对象之操作符重载2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: opencv5-图像混合
- 下一篇: 数据结构二叉树遍历求后序