av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C++賦值函數(shù)代碼詳解

作為一個(gè)經(jīng)驗(yàn)豐富的編程人員,想必對C++編程語言一定有所了解。因?yàn)檫@一語言已經(jīng)成為開發(fā)領(lǐng)域中一個(gè)重要的應(yīng)用語言。下面大家可以根據(jù)本文對C++賦值函數(shù)的理解,進(jìn)一步加深對C++語言的了解程度。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供吳堡網(wǎng)站建設(shè)、吳堡做網(wǎng)站、吳堡網(wǎng)站設(shè)計(jì)、吳堡網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、吳堡企業(yè)網(wǎng)站模板建站服務(wù),十載吳堡做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

C++的拷貝函數(shù)和C++賦值函數(shù)既有聯(lián)系又有區(qū)別,不細(xì)究的話很容易搞混,遂以小例示之如下,權(quán)作解惑之用。

C++賦值函數(shù)相關(guān)代碼示例:

 
 
 
  1. // test.cpp  
  2. #include  
  3. #include  
  4. #include  
  5. using namespace std;  
  6. class Book  
  7. {  
  8. public:  
  9. Book(const char *name, const char*author, const double price): 
    price(price) {  
  10. this->name = new char[strlen(name)+1];  
  11. this->author = new char[strlen(author)+1];  
  12. strcpy(this->name, name);  
  13. strcpy(this->author,author);  
  14. }  
  15. Book(const Book& book){  
  16. name = new char[strlen(book.name)+1];  
  17. author = new char[strlen(book.author)+1];  
  18. price = book.price;  
  19. strcpy(name, book.name);  
  20. strcpy(author, book.author);  
 
 
 
  1. Book& operator=(const Book& rhs) {  
  2. Book(rhs).swap(*this); // 先創(chuàng)建臨時(shí)對象Book(rhs), 
    再調(diào)用下面的swap進(jìn)行數(shù)據(jù)交換,  
  3. // 注意與*this交換數(shù)據(jù)的是臨時(shí)對象, rhs并未修改,只是swap  
  4. // 結(jié)束后臨時(shí)對象擁有了*this的數(shù)據(jù), 而*this也擁有了由rhs  
  5. // 構(gòu)造的臨時(shí)對象的數(shù)據(jù), 臨時(shí)對象生命期結(jié)束時(shí),*this的數(shù)據(jù)  
  6. // 會被銷毀。  
  7. return *this;   
  8. }  
  9. ~Book(){  
  10. delete[] name;  
  11. delete[] author;  
  12. }  
  13. private:  
  14. Book& swap(Book& rhs) {  
  15. double temp = rhs.price;  
  16. rhs.price = price;  
  17. price = temp;  
  18. std::swap(name, rhs.name); 
    // std::swap()只是簡單的交換指針的值  
  19. std::swap(author, rhs.author);  
  20. return *this;  
  21. }  
  22. public:  
  23. char* name;  
  24. char* author;  
  25. double price;  
  26. };  
  27. int main() {  
  28. Book a("The C++ standard library", "Nicolai M. Josuttis", 98);  
  29. Book b = a; // 對象b不存在, 拷貝構(gòu)造函數(shù)在這里被調(diào)用  
  30. Book c("Emacs Lisp manual", "stallman", 0);  
  31. c = a; // c對象已經(jīng)存在, C++賦值函數(shù)(operator=)在這里被調(diào)用  
  32. cout << a.name << endl;  
  33. cout << a.author << endl;  
  34. cout << a.price << endl << endl;  
  35. cout << b.name << endl;  
  36. cout << b.author << endl;  
  37. cout << b.price << endl << endl;  
  38. cout << c.name << endl;  
  39. cout << c.author << endl;  
  40. cout << c.price << endl;  

編譯:

 
 
 
  1. g++ -o test test.cpp 

運(yùn)行結(jié)果:

 
 
 
  1. The C++ standard library  
  2. Nicolai M. Josuttis  
  3. 98  
  4. The C++ standard library  
  5. Nicolai M. Josuttis  
  6. 98  
  7. The C++ standard library  
  8. Nicolai M. Josuttis  
  9. 98 

以上就是對C++賦值函數(shù)的相關(guān)介紹。


新聞標(biāo)題:C++賦值函數(shù)代碼詳解
鏈接URL:http://uogjgqi.cn/article/cooepjd.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流