日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++基础11-类和对象之操作符重载2

發布時間:2025/3/15 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++基础11-类和对象之操作符重载2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

總結:

1、等號操作符重載和拷貝構造函數重載一般用在數據成員中需要單獨在堆區開辟內存時(指針)
2、new,delete重載內部還是使用malloc和free
3、逗號表達式(,)、或者(||),且(&&),條件表達式(?:)具有短路功能。
????? 但重載后失去此功能,故不建議重載這兩個運算符
4、自定義智能指針auto_ptr(在c++11新特性中已經被移除)是一個模板類。
? ? ? 可以自動被回收,自動釋放

等號操作符重載:
?

#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class Student { public:Student() {this->id = 0;this->name = NULL;}Student(int id, char *name) {this->id = id;//this->name = name; 淺拷貝 錯誤this->name = new char[strlen(name) + 1];//this->name = name; 賦值錯誤strcpy(this->name, name);}Student(const Student& another) {this->id = another.id;//深拷貝this->name = new char[strlen(another.name) + 1];strcpy(this->name, another.name);}Student& operator=(const Student& another) {if (this == &another) //防止自身賦值return *this;//先將自身的額外開辟的空間回收掉if (this->name != NULL) {delete this->name;this->name = NULL;this->id = 0;}//執行深拷貝this->id = another.id;this->name = new char[strlen(another.name) + 1]; strcpy(this->name, another.name);return *this;}void printS() {cout << "id:"<<this->id << " name:" << this->name << endl;}~Student() {if (this->name != NULL) {delete this->name;this->name = NULL;this->id = 0;}} private:int id;char *name; };void test01() {Student s1(1, "zhangdan");Student s2 = s1; //調用拷貝構造s2.printS();Student s3(2,"lisi");s2 = s1; //賦值操作符 默認的賦值操作符為淺拷貝 只是簡單的指針指向 //兩次析構 其中一次為空s2.printS(); } /* id:1 name:zhangdan id:1 name:zhangdan */ int main() {test01();return 0; } #endif

?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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。