Constructor, deconstruct, and copy Semantics

Source: Internet
Author: User

If a class already has bitwise copy semantics, implict copy assignment operator is considered useless and will not be merged. This is the same as copy constructor.

A class does not show bitwise copy semantics for the default copy assignment operator in the following cases

1. When the class contains a member object and its class has a copy assignment operator

2. When a base class of a class has a copy assignment operator

3. When a class declares any virtual functions (we cannot copy the vptr address of a class object, because it may be a derived class Object)

4. When the class inherits from a virtual base class (whether the base class has a copy operator or not ),

That is, in the above four cases, there is no bitwise copy semantics, And the compiler needs to automatically synthesize a function.


When bitwise copy semantics is available, bitwise copy does not use copy assignment operator.


When a destructor starts executing an object declaration, the difficulty in deconstruct is that the object transformation will be affected by vptr reconfiguration.

Deconstruct steps:

1. The destructor function book is first executed

2. If the class has the member class objects, and the latter has the Destructors, they will be called in reverse Declaration Order.

3. If the object contains a vptr, It is reset to point to the virtual talbe of the appropriate base class.

4. If any direct (previous layer) nonvirtual base classes has a destructor, they will be called in the declared order of the chamber

5. If any virtual base classes has a destructor, and the class currently discussed is the final class, they will be called in reverse order of their original paparazzi.


The constructor is processed by the compiler in the following sequence:

1. The data members initialization operations recorded in the member initialization list will be put into the constructor function itself, in the declared order of members

2. If a Member does not appear in the member initialization list but has a default constructor, the default constructor must be called.

3. Before that, if the class object has a virtual table constructor, they must be set with initial values pointing to the appropriate virtual table

4. Before that, all the above-level base class constructors must be called in the order declared by the base class (not associated with the order in the member initialization list ):

1) if the base class is listed in the member initialization list, any explicitly specified parameter should be passed.

2) If the base class is not listed in the member initialization list and it has default constructor (or default memberwise constructor), call

3) if the base class is the second or subsequent base class under multi-inheritance, the this pointer must be adjusted.

5. Before that, all virtual base class constructors must be called, from left to right, from deepest to shortest:

1) if the class is listed in the member initialization list, any explicitly specified parameter should be passed. If it is not listed in the list, and the class is represented by a default constructor, it should also be called

2) In addition, the offset of each virtual base class subobject in the class must be accessible during the execution period.

3) if the class object is the underlying class, its constructor may be called, and some mechanisms used to support this behavior must be put in.

Constructor, deconstruct, and copy Semantics

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.