0

I am trying to understand the inner workings of a cpp code. The bit I am focusing is composed of 2 .cpp / .h. The Base class is called Ionization. The Derived class is called IonizationTunnel.

Inside IonizationTunnel.cpp I see the following:

#include "IonizationTunnel.h"
IonizationTunnel::IonizationTunnel( Params &params, Species *species ) : Ionization( params, species ) {
code 
code which uses params and species
code 
}

I understand that it is about IonizationTunnel constructor IonizationTunnel(Params &params, Species *species) which needs to be written with its ''prefix'' IonizationTunnel:: so that it is clear it is coming from IonizationTunnel class.

I do not understand the : Ionization() end-bit. Is it a call to the Base class constructor? If so, why is it there and why isn't it written as :Ionization{params, species}?

Thank you!

velenos14
  • 534
  • 4
  • 13

1 Answers1

1

Yes, it's called a member initializer list. It's the only place you can call the base constructor, there is no super() like in Java or Python.

Both () and {} are ok for initialization: the former performs "direct initialization", the latter performs "direct list initialization".

Their behavior can differ sometimes. E.g. vector<int> v(1, 2) creates a vector of a single element (2), while vector<int> v{1, 2} creates a vector of two elements.

() is more common for constructing base classes as it was there long before C++11. Its behavior is also less ambiguous before C++20: it can only call a base constructor.

yeputons
  • 8,478
  • 34
  • 67