A(int x) : x(x){} what does : x(x){} do?
: x(x)
is the initializer list. The variable in the paranthesis is the argument received while the outer one is the member variable. It means member variable x
is initialized with the value of the x
argument received.
B(int x) : A(2*x)
Here you are calling the base class constructor( i.e, A
) that receives an integer. x
is the variable received by constructor B
. This is a way of calling parameterized base class constructor from derived class constructor. By default, derived class constructor invokes the default base class constructor. In your case, if you don't provide the A(2*x)
it fails because the base class has no default constructor.