溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

C++中有哪些賦值函數(shù)

發(fā)布時(shí)間:2021-07-19 16:18:05 來(lái)源:億速云 閱讀:195 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)C++中有哪些賦值函數(shù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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

  1. // test.cpp  

  2. #include <iostream> 

  3. #include <stdlib.h> 

  4. #include <algorithm> 

  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ì)被銷(xiāo)毀。  

  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;  

編譯:

g++ -o test test.cpp

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

The C++ standard library  Nicolai M. Josuttis  98  The C++ standard library  Nicolai M. Josuttis  98  The C++ standard library  Nicolai M. Josuttis  98

看完上述內(nèi)容,你們對(duì)C++中有哪些賦值函數(shù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI