[C ++ Basics] 021 _ shallow copy and deep copy

Source: Internet
Author: User

Shallow copy: A pointer member variable exists in the class. During the copy, only the pointer variable is copied, but the address block pointed to by the pointer variable is not copied.

Instance code:

1 # include <iostream> 2 using namespace STD; 3 4 class tree {5 public: 6 // copy constructor 7 tree (const Tree & tree) {8 This-> num = tree. num; 9} 10 // constructor 11 Tree () {12 num = new int (10); 13} 14 // destructor 15 ~ Tree () {16 Delete num; 17} 18 // normal print member variable Content Function 19 void printnum () {20 cout <* (this-> num) <Endl; 21} 22 private: 23 int * num; 24}; 25 26 int main () {27 // generate an object 28 tree * tree1 = new tree () in the heap (); 29 tree1-> printnum (); 30 31 // The copy constructor functions 32 tree tree2 (* tree1); 33 tree2.printnum (); 34 35 // destroy the tree1 object 36 Delete tree1; 37 // at this time, the num pointer is 38 tree2.printnum (); 39 40 system ("pause"); 41 return 0; 42}

 

Deep copy: If a pointer variable exists during object copying, the memory block pointed to by the pointer variable is also copied.

Instance code:

1 # include <iostream> 2 using namespace STD; 3 4 class tree {5 public: 6 // copy constructor (deep copy) 7 tree (const Tree & tree) {8 This-> num = new int (); 9 * (this-> num) = * (tree. num); 10} 11 // constructor 12 tree () {13 num = new int (10); 14} 15 // destructor 16 ~ Tree () {17 Delete num; 18} 19 // normal print member variable content function 20 void printnum () {21 cout <* (this-> num) <Endl; 22} 23 public: // For convenience, declare the member variable as public. In fact, you should write a 24 int * num; 25} function to get the member variable }; 26 27 int main () {28 // generate an object in the heap 29 tree * tree1 = new tree (); 30 tree1-> printnum (); 31 32 // The copy constructor functions 33 tree tree2 (* tree1); 34 tree2.printnum (); 35 36 // destroys the tree1 object 37 Delete tree1; 38 // at this time, num is a floating pointer with 39 tree2.printnum (); 40 41 system ("pause"); 42 return 0; 43}

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.