Practice examples for shading in C + +

Source: Internet
Author: User

Class definition, if you do not provide your own copy constructor, C + + provides a default copy constructor, as if no constructor was provided.
C + + provides the same as a default constructor.
The default copy constructor provided by C + + works by completing a copy of a member of a member, and if the member is a class object, call the
Its copy constructor or default copy constructor. /*--------------------------------------
In the default copy constructor, the copied policy is copy-by-member, but a class might have resources if the copy constructor
Simply making a copy of the resource and not assigning it to itself will face a troubling situation: two objects have the same resource
。 When an object is refactored, the resource goes through two resource returns.
The following program describes the confusion when the person object is simply copied and is facing a destructor. --------------------------------------------------*/
#include <iostream>using namespace std; #pragma warning (disable:4996) class Person{public:person (char *pn) {cout  << "Constructing" << pn << endl;pname = new  Char[strlen (PN) + 1];p rintf ("%p\n", pName); if (pName! = 0) {strcpy (pName, PN);}} /*person (const person& p) {cout << "copying" << endl;pname = new Char[strlen (p.pname) + 1];if (pName! = 0) strcpy (PName, P.pname);p rintf ("%p\n", PName);} */~person () {printf ("%p\n", PName) cout << "destructing" << endl;//pname[0] = ';d elete[]  pName;} Protected:char *pname;}; int main () {person   p1 ("Randy"); Person   P2 (p1);               That person  p2 (p1); return 0;}






printf ("%p\n", pName);The memory address that you pointed to is printed.
The above address printing instructions pointer pname point to the same area of memory, so two delete will be a problem.

When the program starts running, the P1 object is created, and the constructor of the P1 object allocates space from the heap and assigns it to the data member PName,
Execution, P2=P1, because the copy constructor is not defined, the default copy constructor is called, making P2 exactly like P1.
There is no new allocated heap space for P2, p1 and p2 pname are the same value. When the P2 is refactored, the string in the heap is cleared into an empty string, and then
Return the heap space to the system, P1, because this is pname points to an empty string, so the third line of output is just destructing,
When executing delete pName; The system should be error-free, but not in GCC
When P2 is created, the object P1 is copied P2, but the resource is not replicated, so P1 and P2 point to the same resource, which is called a shallow copy.
When an object is created and the resource is allocated, it is necessary to define its own copy constructor so that it not only copies the members, but also copies the resources.
#include <iostream>using namespace std; #pragma warning (disable:4996) class Person{public:person (char *pn) {cout  << "Constructing" << pn << endl;pname = new  Char[strlen (PN) + 1];p rintf ("%p\n", pName); if (pName! = 0) {strcpy (pName, PN);}} Person (const person& p) {cout << "copying" << endl;pname = new Char[strlen (p.pname) +1];if (pName! = 0) strc PY (PName, P.pname);p rintf ("%p\n", PName);} ~person () {cout << "destructing" << Endl; Pname[0] = ';d elete[]  pName;} Protected:char *pname;}; int main () {person   p1 ("Randy"); Person   P2 (p1);               That person  p2 (p1); return 0;}



When the P2 is created, the object P1 is copied to P2, and the resources are copied, so P1 and P2 point to different resources, called deep copies.
Heap memory is not the only resource that needs to copy a constructor, but it is the most common one. Open a file and occupy a hard device (such as a printer
Service also requires a deep copy. They are also the types of resources that the destructor must return. So a good lesson is that if your class needs destructors to deconstruct resources, it also needs a copy constructor. Because normally objects are automatically refactored, if a custom destructor is needed, it means that there is additional resources to be used before the object is refactored
Release, at which point the copy of the object is not a shallow copy.
---------------------------------------------*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Practice examples for shading in C + +

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.