C++赋值函数代码详解

开发 后端
我们在实际编程中应当如何正确应用C++赋值函数来进行程序开发?在这篇文章中,将会给出具体的操作应用方法,希望能带给大家一些帮助。

作为一个经验丰富的编程人员,想必对C++编程语言一定有所了解。因为这一语言已经成为开发领域中一个重要的应用语言。下面大家可以根据本文对C++赋值函数的理解,进一步加深对C++语言的了解程度。

C++的拷贝函数和C++赋值函数既有联系又有区别,不细究的话很容易搞混,遂以小例示之如下,权作解惑之用。

C++赋值函数相关代码示例:

  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); // 先创建临时对象Book(rhs), 
    再调用下面的swap进行数据交换,  
  3. // 注意与*this交换数据的是临时对象, rhs并未修改,只是swap  
  4. // 结束后临时对象拥有了*this的数据, 而*this也拥有了由rhs  
  5. // 构造的临时对象的数据, 临时对象生命期结束时,*this的数据  
  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不存在, 拷贝构造函数在这里被调用  
  30. Book c("Emacs Lisp manual", "stallman", 0);  
  31. c = a; // c对象已经存在, C++赋值函数(operator=)在这里被调用  
  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 

运行结果:

  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++赋值函数的相关介绍。

【编辑推荐】

  1. C++遍历集合应用经验总结
  2. C++ include机制基本概念详解
  3. C++ explicit关键字基本内容概述
  4. C++成员函数指针详细使用指南
  5. C++访问控制符内容相关介绍
责任编辑:曹凯 来源: 博客园
相关推荐

2010-01-18 16:17:53

C++代码

2010-02-05 10:23:09

C++基本函数

2023-12-24 12:56:14

C++函数语言

2010-02-04 16:07:39

C++回调函数

2011-08-22 17:13:00

LuaC++函数

2010-02-06 13:42:36

C++单件模式

2021-12-21 15:31:10

C++语言指针

2011-04-20 09:50:45

Virtual

2009-04-14 14:53:06

C++Lambda函数多线程

2010-01-27 17:16:52

C++构造函数

2010-01-26 10:42:26

C++函数

2010-02-02 11:16:28

C++异常

2023-11-09 23:56:21

2024-01-22 10:49:55

C++for循环

2010-02-02 18:01:47

C++字符串替换函数

2023-10-30 10:29:50

C++最小二乘法

2010-01-18 16:56:30

C++函数

2010-01-28 13:35:41

调用C++函数

2010-01-19 13:43:59

C++函数

2011-07-15 01:34:36

C++重载运算符
点赞
收藏

51CTO技术栈公众号