Hi everyone,
I’m working through the tutorial Build a MIDI synthesizer and I am also pretty new to C++. At the moment I am trying to understand how the MainContentComponent is initialised.
As the tutorial states:
Our MainContentComponent
class contains the following data members.
SynthAudioSource synthAudioSource;
MidiKeyboardState keyboardState;
MidiKeyboardComponent keyboardComponent;
The synthAudioSource
and keyboardComponent
members are initialised in the MainContentComponent
constructor.
MainContentComponent()
: synthAudioSource (keyboardState),
keyboardComponent (keyboardState, MidiKeyboardComponent::horizontalKeyboard)
{
...
}
Now from what I can see here, the keyboardState
variable is implicitly initialised by the default MidiKeyboardState constructor.
I have read that
non-static data members are initialized in the order they were declared in the class definition
So how can the synthAudioSource
be initialised with the keyboardState
? Shouldn’t keyboardState
be not-yet initialised since it is declared AFTER the synthAudioSource
member?
I messed around with changing the order of the 3 declarations and it seems to compile fine in Xcode whatever order they are declared in. Is this just some compiler setting that will magically reorder the declarations or what?
The other thing that’s slightly confusing is that we are passing keyboardState
to the initialiser of a SynthAudioSource
instead of a SynthAudioSource
object. I suppose that’s a C++ thing? I get that keyState
is the required constructor argument for SynthAudioSource
, so I assume that it’s a shorthand where providing an argument matching the type expected by the constructor will be passed through to the constructor - kind of like this: (?)
MainContentComponent()
: synthAudioSource (new SynthAudioSource(keyboardState)),
keyboardComponent (new MidiKeyboardComponent(keyboardState, MidiKeyboardComponent::horizontalKeyboard))
{
...
}
Thanks in advance!