Constructor initialization table

Source: Internet
Author: User

In C ++, the constructor has a special initialization method called "initial expression table (initialization table)", which is located after the parameter table of the function, but before the function body, this indicates that the initialization in the table is prior to any code in the function body being executed. The constructor initialization list starts with a colon, followed by a list of data members separated by commas (,). Each data member is followed by an initialization formula in parentheses. Rules:

1. If the class has an inheritance relationship, the derived class must call the base class constructor in its initialization table.

2. Const constants of classes can only be initialized in the initialization table, because they cannot be initialized in the function body by assigning values.

3. Class data member initialization can be completed in the function body or in the initialization table, but the efficiency is different:

1. Non-Internal data types (custom data types) are more efficient when initializing tables.

2. Internal data types (such as int and float) have almost the same efficiency, but the function body assignment makes the program clearer and easier to read.

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.