The C ++ compiler combines the conditions of the default constructor (the default copy constructor is the same), and the constructor copies the constructor.

Source: Internet
Author: User

The C ++ compiler combines the conditions of the default constructor (the default copy constructor is the same), and the constructor copies the constructor.

When a user does not define a constructor for a class, the compiler does not combine each class into a default constructor,The default constructor is synthesized only when the compiler needs it. The synthesis of default constructor by the compiler only meets the compilation needs of the compiler, rather than the needs of programmers (for example, the initialization of member variables also requires programmers to define constructor).

1. The object contains objects with Default constructors

Class A {public: A ();} class B {public: A ;}
In combination, B needs the compiler to synthesize the default constructor to call the default constructor of A to define (does not help programmers to initialize) B's member variables, built-in type variables are not required.

2. The parent class contains the default constructor.

Class A {public: A ();} class B: public {...}
In the inheritance relationship, the compiler needs to call the default constructor A for B's synthesis of constructor classes.

3. classes with virtual functions

For classes with virtual functions, the compiler synthesize a virtual table that stores virtual function pointers as needed.

4. Virtual inheritance class

In the C ++ Single Instance class, why not declare the copy constructor? Will the compiler automatically generate a copy constructor?

If no replication constructor is defined, the compiler will merge one.
Merging and copying constructor: copying members one by one, where value type members directly copy, and class type members call their copying constructor.

If the replication constructor is not defined in the C ++ class, does the system define the default replication constructor?

As you understand, if you do not define a replication constructor, the system will generate a default replication constructor for you, but this is not always a good thing, when your class contains private data members of the pointer type, the default copy constructor is dangerous because it directs the pointers of both objects to the same memory area.

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.