自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
我們?cè)趯?shí)際編程中應(yīng)當(dāng)如何正確應(yīng)用C++賦值函數(shù)來進(jìn)行程序開發(fā)?在這篇文章中,將會(huì)給出具體的操作應(yīng)用方法,希望能帶給大家一些幫助。

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

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

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ì)被銷毀。  
  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)介紹。

【編輯推薦】

  1. C++遍歷集合應(yīng)用經(jīng)驗(yàn)總結(jié)
  2. C++ include機(jī)制基本概念詳解
  3. C++ explicit關(guān)鍵字基本內(nèi)容概述
  4. C++成員函數(shù)指針詳細(xì)使用指南
  5. C++訪問控制符內(nèi)容相關(guān)介紹
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-01-18 16:17:53

C++代碼

2010-02-05 10:23:09

C++基本函數(shù)

2024-04-01 09:13:20

C++函數(shù)遞增

2023-12-24 12:56:14

C++函數(shù)語言

2010-02-04 16:07:39

C++回調(diào)函數(shù)

2011-08-22 17:13:00

LuaC++函數(shù)

2021-12-21 15:31:10

C++語言指針

2011-04-20 09:50:45

Virtual

2010-02-06 13:42:36

C++單件模式

2010-01-27 17:16:52

C++構(gòu)造函數(shù)

2010-02-02 11:16:28

C++異常

2023-11-09 23:56:21

2010-01-26 10:42:26

C++函數(shù)

2024-01-22 10:49:55

C++for循環(huán)

2010-02-02 18:01:47

C++字符串替換函數(shù)

2009-04-14 14:53:06

C++Lambda函數(shù)多線程

2023-10-30 10:29:50

C++最小二乘法

2010-01-18 16:56:30

C++函數(shù)

2010-01-28 13:35:41

調(diào)用C++函數(shù)

2010-01-19 13:43:59

C++函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)