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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++:类中的赋值函数

發(fā)布時間:2025/3/21 c/c++ 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++:类中的赋值函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先來看一個例子:

1 #include<iostream>2 #include<string>3 using namespace std;4 class Student{5 public:6 Student(){7 cout<<"調(diào)用默認(rèn)構(gòu)造函數(shù)"<<endl;8 };9 Student(string name,int age,string gender):Name(name),Age(age),Gender(gender){ 10 //cout<<"調(diào)用構(gòu)造函數(shù)1"<<endl; 11 } 12 Student(const Student& stu){//拷貝構(gòu)造函數(shù) 13 Name=stu.Name; 14 Age=stu.Age; 15 Gender=stu.Gender; 16 cout<<"調(diào)用拷貝構(gòu)造函數(shù)"<<endl; 17 } 18 ~Student(){ 19 //cout<<"調(diào)用析構(gòu)函數(shù)"<<endl; 20 } 21 void show(){ 22 cout<<"Name:"<<Name<<endl; 23 cout<<"Age:"<<Age<<endl; 24 cout<<"Gender:"<<Gender<<endl; 25 } 26 private: 27 string Name; 28 int Age; 29 string Gender; 30 }; 31 32 int main(){ 33 Student stu("Tomwenxing",23,"male"); 34 Student stu2("Ellen",24,"female"); 35 cout<<"---------------------賦值操作之前-----------------"<<endl; 36 stu2.show(); 37 cout<<"---------------------賦值操作之后-----------------"<<endl; 38 stu2=stu; 39 stu2.show(); 40 return 0; 41 }

由上面的例子可以看出,C++支持自定義類型的對象之間的賦值操作,而賦值功能的實現(xiàn)則主要依靠自定義類中的賦值函數(shù)。每一個自定義類中都有且只有一個賦值函數(shù),該賦值函數(shù)既可以由編譯器隱式地定義在自定義類中,也可以有用戶通過對賦值運算符=的重載顯式地定義在自定義類中

1 #include<iostream>2 #include<string>3 using namespace std;4 class Student{5 public:6 Student(){7 cout<<"調(diào)用默認(rèn)構(gòu)造函數(shù)"<<endl;8 };9 Student(string name,int age,string gender):Name(name),Age(age),Gender(gender){ 10 //cout<<"調(diào)用構(gòu)造函數(shù)1"<<endl; 11 } 12 Student(const Student& stu){//拷貝構(gòu)造函數(shù) 13 Name=stu.Name; 14 Age=stu.Age; 15 Gender=stu.Gender; 16 cout<<"調(diào)用拷貝構(gòu)造函數(shù)"<<endl; 17 } 18 ~Student(){ 19 //cout<<"調(diào)用析構(gòu)函數(shù)"<<endl; 20 } 21 Student& operator=(const Student& stu){ //賦值函數(shù) 22 cout<<"調(diào)用類中的賦值函數(shù)"<<endl; 23 if(this!=&stu){ 24 Name=stu.Name; 25 Age=stu.Age; 26 Gender=stu.Gender; 27 } 28 return *this; 29 } 30 void show(){ 31 cout<<"Name:"<<Name<<endl; 32 cout<<"Age:"<<Age<<endl; 33 cout<<"Gender:"<<Gender<<endl; 34 } 35 private: 36 string Name; 37 int Age; 38 string Gender; 39 }; 40 41 int main(){ 42 Student stu("Tomwenxing",23,"male"); 43 Student stu2("Ellen",24,"female"); 44 cout<<"---------------------賦值操作之前-----------------"<<endl; 45 stu2.show(); 46 cout<<"---------------------賦值操作之后-----------------"<<endl; 47 stu2=stu; 48 stu2.show(); 49 return 0; 50 }

特別注意:

Question 1:類中的賦值函數(shù)中的參數(shù)為什么加const?

Answer:參數(shù)使用cosnt的原因有兩個:

? 防止類中的賦值函數(shù)對用來賦值的“原對象”進行修改

1 Student& Student::operator=(Student& stu){2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){ 4 stu.Name="none";//錯誤,對用來賦值的“原對象”進行了修改5 stu.Age=0;//錯誤6 stu.Gender="none";//錯誤 7 Name=stu.Name;8 Age=stu.Age;9 Gender=stu.Gender; 10 } 11 return *this; 12 }

?若賦值函數(shù)的形參加上const,則賦值函數(shù)接受的實參對象既可以是const對象,也可以是非const對象;否則賦值函數(shù)能夠接受的對象只能是非const對象,而不能是const對象。

1 Student& Student::operator=(Student& stu){2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){4 Name=stu.Name;5 Age=stu.Age;6 Gender=stu.Gender;7 }8 return *this;9 } 10 int main(){ 11 const Student stu("Tomwenxing",23,"male"); 12 Student stu2("Ellen",24,"female"); 13 stu2=stu; //錯誤!不能將const對象賦值給非const對象 14 return 0; 15 }

Question 2:類中的賦值函數(shù)中的參數(shù)為什么使用引用?

Answer:避免調(diào)用類中的拷貝構(gòu)造函數(shù)在內(nèi)存中開辟空間來創(chuàng)建形參對象,而是讓形參成為實參的別名,從而節(jié)省時間和空間,提供編程效率

1 Student& Student::operator=(const Student &stu){ //參數(shù)中使用引用 2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){4 Name=stu.Name;5 Age=stu.Age;6 Gender=stu.Gender;7 }8 return *this;9 } 10 11 int main(){ 12 const Student stu("Tomwenxing",23,"male"); 13 Student stu2("Ellen",24,"female"); 14 stu2=stu; 15 return 0; 16 }

1 Student& Student::operator=(const Student stu){ //參數(shù)中沒有使用引用 2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){4 Name=stu.Name;5 Age=stu.Age;6 Gender=stu.Gender;7 }8 return *this;9 } 10 11 int main(){ 12 const Student stu("Tomwenxing",23,"male"); 13 Student stu2("Ellen",24,"female"); 14 stu2=stu; 15 return 0; 16 }

Question 3:類中的賦值函數(shù)的返回值類型為什么是Student&,不可以是Student或void嗎?

Answer:在C++中,系統(tǒng)支持變量之間的連續(xù)賦值,如:

1 int a=10; 2 int b,c; 3 b=c=a;//變量之間的連續(xù)賦值,b、c的值均為10

其本質(zhì)是先將變量a的值賦值給變量c,然后將賦值后的變量c返回到賦值運算符=的右邊,再將其賦值給變量b,即:

1 int a=10; 2 b=(c=a); //即c=a,b=c;

同理,如果類中賦值函數(shù)的返回值類型為void,即類中的賦值函數(shù)沒有返回值,此時編譯器將不支持對該類中的對象進行連續(xù)賦值

1 void Student::operator=(const Student &stu){2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){4 Name=stu.Name;5 Age=stu.Age;6 Gender=stu.Gender;7 }8 }9 int main(){ 10 const Student stu("Tomwenxing",23,"male"); 11 Student stu2("Ellen",24,"female"); 12 Student stu3; 13 stu3=stu2=stu; //錯誤!stu3=stu2=stu相當(dāng)于stu3.operator=(stu2.operator=(stu)) ,由于賦值函數(shù)沒有返回值,則該語句無法執(zhí)行 14 return 0; 15 }

?而賦值函數(shù)的返回值類型之所以是Student&而非Student是為了避免函數(shù)在返回對象時調(diào)用類中的拷貝構(gòu)造函數(shù)在內(nèi)存中創(chuàng)建臨時對象,從而提高程序的運行效率

1 Student& Student::operator=(const Student& stu){ //返回值類型中使用引用& 2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){4 Name=stu.Name;5 Age=stu.Age;6 Gender=stu.Gender;7 }8 return *this;9 } 10 11 int main(){ 12 Student stu("Tomwenxing",23,"male); 13 Student stu2; 14 stu2=stu; 15 return 0; 16 }

1 Student Student::operator=(const Student& stu){ //返回值類型中沒有使用引用& 2 cout<<"調(diào)用類中的賦值函數(shù)"<<endl;3 if(this!=&stu){4 Name=stu.Name;5 Age=stu.Age;6 Gender=stu.Gender;7 }8 return *this;9 } 10 11 int main(){ 12 Student stu("Tomwenxing",23,"male"); 13 Student stu2; 14 stu2=stu; 15 return 0; 16 }

Question 4:語句“if(this!=&stu)”的作用是什么?

Answer:通過比較賦值者和被賦值者的地址是否相同來判斷兩者是否是同一個對象,從而避免自賦值(即自己給自己賦值)的情況的發(fā)生,原因如下:

? 為了提高程序的運行效率。自己給自己賦值是完全無意義的行為,只會浪費程序的時間資源。

? 當(dāng)類中的數(shù)據(jù)成員中含有指針時,自賦值操作可能會帶來災(zāi)難性的后果。例如假設(shè)對象a和b中都含有一個指針ptr,它們分別指向一塊通過new動態(tài)開辟的內(nèi)存空間A和內(nèi)存空間B,當(dāng)將對象a賦值給對象b時,要求先將對象b中指針ptr指向的內(nèi)存空間B通過delete釋放掉(否則將造成內(nèi)存泄漏),然后在內(nèi)存中重新開辟內(nèi)存空間C以拷貝內(nèi)存空間A中的內(nèi)容,并讓對象b的指針ptr重新指向內(nèi)存空間C,從而完成賦值。如果此時允許對象的自賦值,那么對象會在自賦值前先釋放自己指針?biāo)傅膬?nèi)存空間,然后重新在內(nèi)存中開辟空間,然而由于之前的內(nèi)存空間已經(jīng)釋放,此時新內(nèi)存空間希望拷貝的內(nèi)容將不復(fù)存在,因而造成災(zāi)難性后果。

因此,對于類中的賦值函數(shù),一定要先檢查是否是自賦值,如果是,直接return *this。

Question 5:自定義類的對象之間的賦值操作的本質(zhì)是什么?

Answer:本質(zhì)是調(diào)用類中的成員函數(shù)(operator=()函數(shù))。如:

1 #include<iostream>2 #include<string>3 using namespace std;4 class Student{5 public:6 Student(){7 //cout<<"調(diào)用默認(rèn)構(gòu)造函數(shù)"<<endl;8 };9 Student(string name,int age,string gender):Name(name),Age(age),Gender(gender){ 10 //cout<<"調(diào)用構(gòu)造函數(shù)1"<<endl; 11 } 12 Student(const Student& stu){//拷貝構(gòu)造函數(shù) 13 Name=stu.Name; 14 Age=stu.Age; 15 Gender=stu.Gender; 16 cout<<"調(diào)用拷貝構(gòu)造函數(shù)"<<endl; 17 } 18 ~Student(){ 19 //cout<<"調(diào)用析構(gòu)函數(shù)"<<endl; 20 } 21 Student& operator=(const Student& stu){ //賦值函數(shù) 22 cout<<"調(diào)用類中的賦值函數(shù)"<<endl; 23 if(this!=&stu){ 24 Name=stu.Name; 25 Age=stu.Age; 26 Gender=stu.Gender; 27 } 28 return *this; 29 } 30 void show(){ 31 cout<<"\tName:"<<Name<<endl; 32 cout<<"\tAge:"<<Age<<endl; 33 cout<<"\tGender:"<<Gender<<endl; 34 } 35 private: 36 string Name; 37 int Age; 38 string Gender; 39 }; 40 41 int main(){ 42 Student stu("Tomwenxing",23,"male"); 43 Student stu2,stu3; 44 stu2=stu;//對stu2進行賦值 45 cout<<"對象stu2:"<<endl; 46 stu2.show(); 47 cout<<"------------分界線-------------------"<<endl; 48 stu3.operator=(stu);//對stu3進行賦值 49 cout<<"對象stu3:"<<endl; 50 stu3.show(); 51 return 0; 52 }

總結(jié)

以上是生活随笔為你收集整理的C++:类中的赋值函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。