C ++ initialization table

Source: Internet
Author: User

I am a person who is easy to forget. I don't know if I have learned C ++ initialization table before, or why I have never used classes I have written; in my mind, a method to call the constructor of the parent class is to add a colon after the sub-class constructor, but I don't know its name, today I did not expect to deepen my understanding of C ++, so I wrote this article to record the role of C ++'s initialization table.

To mention the important role of table initialization, I found the two common ones online:

(1) If the class has an inheritance relationship, the derived class must call the base class constructor in its initialization table.
(2) The const constant of the class can only be initialized in the initialization table, because it cannot be initialized in the function body by assigning values.

Example:

Class

{

Const int TMP; // constant in Class

Public A (INT value): TMP (value) // use the initialization table to initialize the constant TMP

{
}
};

Class B

{

Public B (INT bvalue): A (bvalue) // use the initialization table to call the constructor of the parent class

{
}
};

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.