日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C++賦值函數(shù)代碼詳解

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

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供吳堡網(wǎng)站建設(shè)、吳堡做網(wǎng)站、吳堡網(wǎng)站設(shè)計(jì)、吳堡網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(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í)對(duì)象Book(rhs), 
    再調(diào)用下面的swap進(jìn)行數(shù)據(jù)交換,  
  3. // 注意與*this交換數(shù)據(jù)的是臨時(shí)對(duì)象, rhs并未修改,只是swap  
  4. // 結(jié)束后臨時(shí)對(duì)象擁有了*this的數(shù)據(jù), 而*this也擁有了由rhs  
  5. // 構(gòu)造的臨時(shí)對(duì)象的數(shù)據(jù), 臨時(shí)對(duì)象生命期結(jié)束時(shí),*this的數(shù)據(jù)  
  6. // 會(huì)被銷毀。  
  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()只是簡(jiǎn)單的交換指針的值  
  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; // 對(duì)象b不存在, 拷貝構(gòu)造函數(shù)在這里被調(diào)用  
  30. Book c("Emacs Lisp manual", "stallman", 0);  
  31. c = a; // c對(duì)象已經(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 

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


本文名稱:C++賦值函數(shù)代碼詳解
標(biāo)題路徑:http://www.dlmjj.cn/article/cooepjd.html