Conditions for the C + + compiler to synthesize default constructors and copy control members (copy constructors, assignment operators, destructors)

Source: Internet
Author: User

(Refer to the in-depth understanding of the C + + object model)

"There are two common misconceptions about C + + newbies:

Any class that does not have a default constructor defined will be synthesized.

The default constructor compiled by the compiler will explicitly set the defaults for each data member in the class. "

Now the main explanation is why the first one is wrong, according to the "in-depth understanding of the C + + object Model", "default constructor is generated by the compiler when needed", the following are 4 kinds of "time needed":

1). The class contains a member object and the latter has a default constructor.

If a class does not have any constructors, but it contains a member function that contains a default constructor, then the compiler needs to synthesize a default constructor for the class and invoke the member's default constructor.

2). The class inherits from a base class and the latter has a default constructor.

Principle and 1) similar

3). This class has a virtual function

The compiler needs to synthesize a default constructor and two expansion operations occur during compilation: "A virtual function table (known as VTBL in Cfront) is generated by the compiler, which puts the virtual function address of class", " In each class object, an additional pointer member (that is, vptr) is synthesized by the compiler, containing the associated class VTBL address ".

4). The class derives from an inherited chain of strings, where one or more virtual base classes

Different compilers implement different implementations of virtual base classes, but the compiler needs to synthesize a default constructor and change the code that "performs access operations" on the virtual base class so that the operation of the virtual base class is deferred until the execution time is determined.

Similarly, if the class does not have a copy constructor defined, then the compiler will see if the class has "bitwise copy semantics" (Bit-by-copy semantics to decide whether to synthesize copy constructors), and in the following four cases the class does not show "bitwise copy Semantics" :

1). The class contains a member object and the latter has a copy constructor.

2). The class inherits from a base class and the latter has a copy constructor.

3). This class has a virtual function

4). The class derives from an inherited chain of strings, where one or more virtual base classes

It is visible that the compiler's requirement to synthesize a copy constructor when the class does not have a copy constructor is similar to the default constructor, except that if the compiler does not synthesize a default constructor, no initialization will be performed on the member, and if the compiler does not synthesize the copy constructor, the bitwise Copy "(Bit successive copy, bit-by-bit copy), if the member has pointers, then the bit successive copy is a shallow copy.

Conditions for the C + + compiler to synthesize default constructors and copy control members (copy constructors, assignment operators, destructors)

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.