C + + constructor principles

Source: Internet
Author: User

Today is nothing to see the C + + constructor principle, by the way to make a note, after all, good memory is not as bad as the written, nonsense not much to say, straight to the subject.

The C + + constructor principle boils down to a few of the following:

1. If a subclass does not have a constructor defined, the child class defaults to calling the parent class parameterless constructor.

2, if a subclass defines a constructor, regardless of whether the constructor of the subclass has parameters or no arguments, when the subclass is created, the constructor that calls the parent class is not displayed, and the parameterless constructor of the parent class is called first, and the subclass's own constructor is called.

3, when creating subclasses, if the subclass does not display the constructor that calls the parent class, the parent class defines its own parameterless constructor, then the parent class is called without a parameter.

4. When creating a subclass object, if the subclass does not display a constructor that calls the parent class, and the parent class simply defines the constructor with the parameter, an error is made. The correct approach is that the subclass must display a parameterized constructor that invokes the parent class.

5. When creating a subclass object, the subclass must initialize the parent class constructor with the initialization class table if the child class displays the constructor that called the parent class.

Above, it boils down to that the subclass must first call the parent class's constructor, and then call its own constructor.

C + + constructor principles

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.