After a bit of searching it looks like I have a couple options. 1) Declare with default constructor and then call an init function in the editor constructor. 2) Declare with a pointer and then call new.
Option 1 is often recommended online but seems awfully clunky and kind of defeats the purpose of a constructor. But Option 2 allocates on the heap and requires manual cleanup.
I’m coming from Python where I never have to deal with this stuff. Am I overthinking it? Does it matter? In general if I want to modularize GUI components, say for a multiband EQ where each band has its own gain, Q, frequency, shape settings, what is the cleanest way forward if I want to parameterize objects instead of copy-pasting code in the editor file?
If you know the arguments you want to pass to the Knob constructor at the time the editor is constructed, then there’s no reason to delay construction of the Knob object or use 2-stage initialization, just pass those parameters directly to the Knob constructor:
// editor.h:
struct Editor : public juce::AudioProcessorEditor
{
Knob knob;
};
// editor.cpp:
Editor::Editor
: knob("This knob's suffix") // this argument gets passed to the Knob constructor
{
}
if you have a reason to do defer like this you should instead declare as std::unique_ptr<Class> and then use std::make_unique<Class>(...args...) to assign to it.
It’s very rare that you should ever need to call new in modern C++.
Thanks. I thought I had to do everything within the curly brackets. I don’t fully understand what is happening, syntactically, in the second line of editor.cpp where knob is constructed. In the header declaration the colon denotes inheritance, but in the constructor in editor.cpp it’s used to initialize members?