Private vs. public member instance declaration

In the Tutorial: Introduction to DSP, what is the reason for declaring the processorChain template type in the private section and not in the public section of the CustomOscillator class? Is there an example where we might want to use it as a public member?

Here:

class CustomOscillator
{
public:
    //==============================================================================
    CustomOscillator() {}
//...
private:
    //==============================================================================
    juce::dsp::ProcessorChain<juce::dsp::Oscillator<Type>, juce::dsp::Gain<Type>> processorChain; // [1]
};

Not here:

class CustomOscillator
{
public:
    //==============================================================================
    CustomOscillator() {}
    juce::dsp::ProcessorChain<juce::dsp::Oscillator<Type>, juce::dsp::Gain<Type>> processorChain; // [1]

I have some experience with C++ but some concepts are escaping me right now…

Thanks in advance…

Mike

It doesn’t make any difference for how the code works once compiled, but private (or protected) members make it clear they shouldn’t be directly touched by code external to the class.

1 Like

protected stuff has this nice property that it’s in fact available in derived classes, unlike private stuff. so i’d say: private if you’re sure no one’s ever going to touch it, protected for things that could or should be redefined later and public if setting and getting it without an extra function doesn’t break the object’s functionality

1 Like

thanks for the quick feedback!

Another point of view if you are managing a bigger codebase shared by multiple projects is, that you are highly interested to not change the public interface of an existing class in a breaking way. Everything declared private can however be completely reworked at any time and everything declared private can be made public at any time without the risk of breaking something. So only declaring the functions and members that really need to be accessed from outside the class makes your life easier in future maintenance.

Furthermore you might want to be sure that certain variables are private and only accessible via specific public getter and setter functions. Even if they only forward access to a private member in your first version you can easily add more functionality like sanity checks to those functions later on. And a big plus when debugging: If the only way a member variable can be modified is through a public setter to a private variable, you can easily put a breakpoint into that setter function and trace every write access to the variable, which might come in handy in certain situations.

4 Likes