When does the C ++ compiler provide the default constructor for users?

Source: Internet
Author: User

The first type is that the class member has a member as a class object and the member's class contains the default constructor. Then, the C ++ compiler will help you generate a default constructor for this class, it is used to call the constructor of its member object to complete the initialization structure of the member. It should be emphasized that if the member's class does not provide the default constructor, the C ++ compiler will not help you generate the default constructor of the class.

The second case is that the base class of this class has the default constructor. The C ++ compiler will also help you generate the default constructor of the derived class to call the default constructor of the base class and complete the initialization of the base class. In addition, it should be emphasized that if the base class does not provide this default constructed function, the C ++ compiler will not generate the default constructor for the derived class (here there are two meanings: first, the base class does not have any form of constructor. Second, the base class has other forms of non-default constructor. Of course, this type is not compiled, and the truth is obvious ).

The third case is that there are virtual functions in the class, then the C ++ compiler will generate the default constructor for you to initialize the virtual table (virtual function table vftable ).

The fourth case is that there is a virtual base class, so the C ++ compiler will generate a default constructor for you to initialize the virtual base class table (vbtable ).

In the following example, the compiling fails because the default constructor is not provided in the base class A. After adding the default constructor to the base class A, the compilation passes ..

# Include "iostream" using namespace STD; Class A {public: A () {}// the default constructor can be added to the base class. A (int c) {x = C; cout <"A" <Endl;} void fun () {cout <"in a fun" <Endl;} PRIVATE: int x ;}; Class B: Public A {public: B (INT c) {Y = C; cout <"B" <Endl ;} void fun () {cout <"in B fun" <Endl;} PRIVATE: int y ;}; int main (void) {A * A = new B (2 ); system ("pause"); Return 0 ;}

 

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.