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
{
}
};