Class constructor initialization table

Source: Internet
Author: User
Class constructor has an initialization table located after the function parameter table, before the function body, anyCodeBefore Execution, use the following rules:
1. If there is an inheritance relationship between the two classes, the derived class must call the base class constructor in the initialization table. The inherited Derived classes are implicitly called in the function initialization table.
If the base class: class base {public: Base ()}; the derived class diversed inherits base:
Class diversed: Base {public diversed ()}; The diversed constructor can be written in two ways: diversed (){......}; Or diversed: Base (){......}; The two methods are actually the same, but one is implicit call and the other is explicit call.

2. Const constants can only be initialized in the function initialization table. Because const constants must be initialized during declaration, but no space is allocated during class declaration, class members can be assigned a value only when class objects are created, therefore, this task is handed over to the initialization table of the class constructor.

3. Data member initialization can be performed in two ways: the initialization table of the class constructor or the member function body, but the two efficiency is different. If it is an internal data type, the efficiency of the two methods is the same, but the readability in the member function body is better, so it should be initialized in the member function body. Non-Internal data types should be initialized in the initialization table.

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.