That Thing is called a Member Initializer List in C++.
There is a difference between Initializing a member using initializer list(2nd Example) and assigning it an value inside the constructor body(1st Example).
When you initialize fields via initializer list the constructors will be called once. The object gets constructed with the passed parameters.
If you use the assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.
As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.
How are the names in the list after construct thing (I have no idea how its called) resolved?
public:
some_type B;
A(some_type B) : B(B)
{
}
In the above snipet, there are two entities named B:
- First is the one that constructor receives as an argument &
- Second is the member of the class
A
The variable received as argument in Constructor of A gets passed as an argument for constructing B(by calling its constructor) which is member of class A. There is no ambiguity in names here, the all to constructor is:
this->B(B);
this is class A pointer.
B() is constructor of type B.
B inside the parenthesis is an instance of type B.