Default constructor in C ++

Source: Internet
Author: User

> When compiler automatically generates default constructor

Speaking of the default constructor in C ++, Let's see when the default constructor will be generated.

1. A class member has a member as a class object, and the member's class contains the default constructor.

The C ++ compiler will help you
This class also generates a default constructor to call the constructor of its member object to complete the initialization of the member. Note that if the member's class does not provide the default constructor
The C ++ compiler will not help you generate the default constructor for this class.

So for the pod type struct, we can think of it as C-style struct.

2. 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.

If the base class does not provide the default constructor, the C ++ compiler will not generate the default constructor for the derived class.

3. If there is a virtual function in the class, the C ++ compiler will generate the default constructor for you to initialize the virtual table (vftable ).

4. If there is a virtual base class, the C ++ compiler will generate the default constructor for you to initialize the virtual base class table (vbtable ).


> Defaulted constructor in C ++ 11

C ++ 11 adds a default keyword

 

Struct sometype
{
Sometype () = default; // The default constructor is explicitly stated.
Sometype (othertype value );
};

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.