Juce Component is not copyable so you can’t pass them around as values. Use a pointer or reference to the component instead. In that case the passed component of course has to live as long as the object where it was passed. (So don’t have the component for example as a local variable anywhere.)
Here using a reference (note that the default constructor has to be removed because of the reference member, references always have to be initialized) :
So as soon as a class has a member that is a reference type then it must be initialized from the constructor and that removes any possibility of an empty default constructor?
If you need to be able to create the object with the default constructor and only later make it point to the other object, you can use a pointer member for the object. (TextButton* member instead of TextButton& in your case.)
If the constructors and/or copy assignment operators are private, the object becomes non-copyable. Juce has a macro to achieve that and many of the Juce classes use that to prevent typical mistakes.
Another option is to have the button be a std::optional<TextButton>. Then, you can default-construct it as nullopt, and construct it properly later on.
hmm, thanks that is interesting. I was just curious about still having the default constructor and usually prefer to make sure an object is ‘fully constructed’ from the get-go but it’s interesting to hear of other possibilities
Sure, that’s a possibility! So instantiate a TextButton inside the DrumPad object? To be honest, I’m just trying to work my way through some of the tutorials but I keep wandering off course trying to modify the examples.
Yes. As in your initial code example, having TextButton as a member of DrumPad has already instantiated it. It’s more a matter of where it is configured. But yes, do all of that in the DrumPad class, since it seems like the DrumPad owns the TextButton.
hehe, i’m afraid that I’m not being very structured right now - i’m just trying to get a grasp on how C++ objects are put together - sorry
But could you show an example of how to define the class such that I could configure the TextButton after instantiating the DrumPad? More like this perhaps…
That class wouldn’t work at all, because the TextButton object isn’t initialized anywhere. If you want to make the DrumPad object own the TextButton, you could probably just have it as a value member. (That would make the DrumPad object itself non-copyable also, though…)
private:
TextButton myButton;
And why not make the DrumPad object initialize the TextButton (text etc) as needed?