Chapter Two The semantics of the constructor when does the compiler synthesize a copy constructor? __ function

Source: Internet
Author: User

The difference between a bit copy (a shallow copy) and a value copy (deep copy) should be made clear first (reference http://blog.sina.com.cn/s/blog_a2aa00d70101gpvj.html). A bit copy, and "bitwise copy" means to copy the memory image of an object to another object, simply by bit; A value copy means to copy the value of the original object to a new object. When you use "bitwise Assignment", the object's memory image is copied directly to another object, so that two objects point to the same memory area, and when one object is freed, the other object's pointer becomes a null pointer.

In general, a good compiler can generate bitwise copies for most class object, but when you do not have to "bitwise copies", the compiler produces the default constructor and copy constructor, and the following four cases do not require " Bitwise copies "(Consistent with the previous subsection, just replace the default constructor with a copy constructor):

1 if a class does not have a copy constructor, but contains a member variable of a class type that contains a copy constructor, the compiler will then synthesize a copy constructor for the class;

2 If a class does not have a copy constructor, but the class inherits from the base class containing the copy constructor, the compiler then synthesizes a copy constructor for the class;

3 If a class does not have a copy constructor, but the class declares or inherits the virtual function, the compiler will then synthesize a copy constructor for the class;

4 If a class does not have a copy constructor, but the class contains a virtual base class, the compiler will then synthesize a copy constructor for the class;

The third situation explains the second issue of the first chapter, "on objects", because it is not in the semantic context of "bitwise copies" At this time, the compiler generates a copy constructor in which the virtual function pointer still points to the virtual function table address of the class, such as (reference):

Automatically generates a copy constructor for Class A, vptr the Class A object in the function, roughly as follows:

A::A (const a& A)

{

The virtual function table address of the This->vptr=a class;
}

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.