C ++ constructor and destructor (copy constructor)

Source: Internet
Author: User

Header file
Class student
{
Public:
Student (char *);
~ Student ();
Student (const student &);
Char * Name;
Static int num;
};

 Main. cpp File

Int Student: num = 0;

Student: Student (char * myname)

{

Num ++;

Int Len = strlen (myname );

Name = new char [Len + 1];

Strcpy (name, myname );

Cout <name <":Create, remaining number:"<Num <Endl;

}

Student ::~ Student ()

{

Num --;

Cout <name <":Destroyed, remaining quantity:"<Num <Endl;

Delete [] Name;

}

Void fun (student funs)

{

Cout <funs. Name <"Called"<Endl;

}
Void main ()

{

Student stu1 ("student1"); // -------------- 1

Student stu2 ("student2"); // -------------- 2

Student stu3 ("student3"); // -------------- 3

 

Fun (stu2); // -------------- 4

Student stu4 = stu3; // -------------- 5

Student stu5 ("student5"); // -------------- 6

Stu5 = stu1; // -------------- 7

}

1,2,3Are normal,4The real parameter is a class value object, and the transfer process is a copy constructor,Funs in funAfter the method is completed, the Destructor will be called and run5This is an instantiation class by copying constructors. This process does not call constructor and destructor, because we need to know how to write the copy constructor, as shown below:
Student (const student & C)

{

Name = C. Name;

}

So5It is equivalentStudent stu4 (stu3)

To7Only assign values, not instantiate classes by copying constructors, becauseStu5Already in6Is instantiated.

ThisCodeInVvc 6.0A magic error occurred while running. The first error isMainAfter the function is completed, the hierarchy is one by one. The hierarchy sequence of the stack is advanced, followedStu5, And then analyze the structureStu4, And then analyze the structureStu3In the structureStu3An error occurred. As follows:


Why is it wrong? After research, we found that5This location calls the copy constructor to constructStu4Class Object, based on the prototype of the copy constructor (below is the prototype)
Student (const student & C)

{

Name = C. Name;

}

I found Name = C. Name , 5 That is Stu4.name = stu3.name; Name Is a pointer, which is a pointer value, that is Stu4 Of Name Pointer pointing Stu3 The position pointed to by the pointer. Then the problem is solved. Stu4 (Because of the stack structure order, the first structure is late and pushed to the stack ), Delete [] Name That is, tell the computer, Name This memory is released and not directed by anything (not related to anything ). Stu3 A problem occurs .. You have nothing to do with this address. Delete . And there is no need Delete But if you know the address of the space and can directly access it through the address Name Because the value is not deleted, after you modify the code, you will better understand where the problem is. 5 And 6 Add the following sentence: Cout <"stu3 Of Name Pointing address: "<(Int *) stu3.name <" \ t "<" stu4 Of Name Pointing address: "<(Int *) stu4.name <Endl;
Output (do not directly cout <"stu3 Of Name Pointing address: "< & Stu3.name <"\ t" <"stu4 Of Name Pointing address: "< & Stu4.name <Endl; in this way, this is to obtain the address of the pointer in the stack, rather than the address of the content stored in the heap)

Solve this problem .. It seems that only the copy constructor is overwritten (The following is the reconstruction of the copy constructor)

Student: Student (const student & tempstu)

{

Int Len = strlen (tempstu. Name );

Name = new char [Len + 1];

Strcpy (name, tempstu. Name );

}
Solve all problems, but when running

Obviously, this is in the Structure1Is the structureStu1When7SentenceStu5 = stu1; this is only a value assignment process, because stu5The object is in6The sentence is constructed. If the value assignment process is stillStu5.name = stu1.nameAnd the cause is the same as above. This is the so-called shallow replication process, which is also a problem that is easily caused by the shallow replication (you can study the shallow replication and deep replication)

Why programming the final number of remaining entries in the Structure -2 (In fact, this example imitates C ++ Primer ), Mainly because of the location 4 Called Fun (stu2 );- This call passes a real Parameter Stu2 Is similar
Student funs = stu2 ; This Funs Object in Fun The method is destroyed when it is returned, and the Destructor is called when it is destroyed. Num -- And in 5 Location, through Student stu4 = stu3 this copy constructor to instantiate stu4 In the rewritten copy constructor Num ++, However Num -- That's why Num Does not return to the initial stage 0 , -2

Related Article

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.