Initialization List is another or a better approach for initializing the member variables (and base class objects) of a class upon construction of an instance of it's own.
Things to know about Initialization List
Initialization List is used to initialize both user defined data types (like embedded object of a class) and also primitive/built-in data types (like int, char).
Initialization List can appear irrespective of the place the constructor is defined.
Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class.
Data members are initialized in the order they are declared, regardless of the order of their initialization.
It is mandatory to initialize Reference Data Member in an Initialization List because it can not exist without being initialized.
It is mandatory to initialize Constant Data Member in an Initialization List otherwise it would be constrcuted with some junk values and we cannot initialize it later anywhere else.
It is mandatory to construct and initialize, embedded class objects/base class objects in case of inheritance, in an Initialization List, if they do not themselves have a zero-argument/default constructor provided.
*************************************************************************************
Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class.
There are two steps that takes place when Member Objects of a class are initialized inside the body of a constructor.
1. Member Objects are allocated a memry/constructed and are given default values by the time when the control enters body of the constructor.
2. Later on the actual initialization happens inside the body of the constructor i.e. user written initilization code that gets called.
Here there's an unnecessary first step where the member objects of a class gets constructed and are given a default value. An initialization list avoids this step and can make your code execute faster as a result.
Data members are initialized in the order they are declared, regardless of the order of their initialization.
It is important to know that the order of initializing the member variables in the Initialization List must match the order of their declarations inside the class. The reason is that the compiler automatically transforms the Initialization List so that it coincides with the order of the declaration of class members inside the class.
*************************************************************************************
In any constructor that initializes member objects, it can pay big dividends to set the objects using an initialization list rather than within the constructor itself. Why? Class member variables are automatically constructed using their default constructor prior to entry within the class constructor itself. You can override this behavior by specifying a different member constructor (usually a copy constructor) in the initialization list. Multiple initializations are separated with commas (not shown here).
template
{
T m_Value;
public:
// no list
CtorInit(const T& t) // m_Value default ctor called here automatically
{
m_Value = t; // m_Value assignment operator called
}
// with list
CtorInit(const T& t) : m_Value(t) { } // m_Value copy ctor called
};
*************************************************************************************
No comments:
Post a Comment