C++基础11-类和对象之操作符重载2
生活随笔
收集整理的這篇文章主要介紹了
C++基础11-类和对象之操作符重载2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
總結:
1、等號操作符重載和拷貝構造函數重載一般用在數據成員中需要單獨在堆區開辟內存時(指針)
2、new,delete重載內部還是使用malloc和free
3、逗號表達式(,)、或者(||),且(&&),條件表達式(?:)具有短路功能。
????? 但重載后失去此功能,故不建議重載這兩個運算符
4、自定義智能指針auto_ptr(在c++11新特性中已經被移除)是一個模板類。
? ? ? 可以自動被回收,自動釋放
等號操作符重載:
?
?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操作符 依然會觸發對象的構造函數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);//等價于//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];//等價于//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 << "執行了+操作符重載" << endl;Test temp(this->value + another.value);return temp;}bool operator&&(Test &another){cout << "執行了&&操作符重載" << 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);//重載&&操作符,并不會發生短路現象。if (t1 && t2) { //t1.operator&&(t2)cout << "為真" << endl;}else {cout << "為假" << endl;} } /* 執行了&&操作符重載 為假 ~Test()... ~Test()... */ void test02() {int a = 0;int b = 20;if (a && (a = 10)) { //a為0的話,a+b 就不會執行 短路cout << "true" << endl;}cout << "a:" << a << endl;Test t1(0);Test t2(20);//重載&&操作符,并不會發生短路現象。if (t1 && (t1 + t2)) { //t1.operator&&(t1.operator+(t2))cout << "為真" << endl;}else {cout << "為假" << endl;} } /* a:0 執行了+操作符重載 ~Test()... 執行了&&操作符重載 ~Test()... 為假 ~Test()... ~Test()... */ void test03() {Test t1(0);Test t2(20);//重載||操作符,并不會發生短路現象if (t1 || (t1 + t2)) {//t1.operator||( t1.operator+(t2) )cout << "為真" << endl;}else {cout << "為假" << endl;} } /* 執行了+操作符重載 ~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中模板類,指針用完自動回收,不需要手動delete):
#if 1 #include<iostream> using namespace std; #include<memory> //智能指針是自動被回收,自動釋放 //只能指針是一個模板類 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;//等價于auto_ptr<int> ptr(new int);*ptr = 100; } void test03() { #if 0A* ap = new A(10);ap->func();(*ap).func();delete ap; #endif//等價于auto_ptr<A> ap(new A(10));ap->func();(*ap).func(); //智能指針用完后 自動回收 調用析構函數 } /* 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表示返回對象本身} private:A *ptr; //內部指針 }; 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?
總結
以上是生活随笔為你收集整理的C++基础11-类和对象之操作符重载2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv5-图像混合
- 下一篇: s3c2440移植MQTT