Detailed description of C ++ assignment function code

Source: Internet
Author: User

As an experienced programmer, I must be familiar with the C ++ programming language. This language has become an important application language in the development field. Next, you can further understand the C ++ language based on the understanding of the C ++ assignment function in this article.

C ++'s copy function and C ++'s value assignment function have both links and differences. It is easy to confuse them without further research. The following is an example to illustrate how to use the function to solve the problem.

Example code of the C ++ assignment function:

 
 
  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); // create a temporary object Book (rhs) first ),
    Call the following swap for data exchange,
  3. // Note that a temporary object is used to exchange data with * this. rhs is not modified, but swap
  4. // After the end, the temporary object has * this data, and * this also has
  5. // Construct the data of the temporary object. * this data is generated when the life cycle of the temporary object ends.
  6. // It will be destroyed.
  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 () is just a simple exchange of pointer values
  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", "niclai M. josutis", 98 );
  29. Book B = a; // object B does not exist. The copy constructor is called here.
  30. Book c ("Emacs Lisp manual", "stallman", 0 );
  31. C = a; // The c object already exists. The C ++ assignment function (operator =) is called here.
  32. Cout <a. name <endl;
  33. Cout <a. author <endl;
  34. Cout <a. price <endl;
  35. Cout <B. name <endl;
  36. Cout <B. author <endl;
  37. Cout <B. price <endl;
  38. Cout <c. name <endl;
  39. Cout <c. author <endl;
  40. Cout <c. price <endl;
  41. }

Compile:

 
 
  1. g++ -o test test.cpp 

Running result:

 
 
  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 

The above is an introduction to the C ++ assignment function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.