Adding a class as base class - you can do that!?

#1

In a bunch of the tutorials you often see something along the lines of:

We have also added the Slider::Listener class as base class, so that we can register our class to receive slider changes

class MainContentComponent   : public Component,
                               public Slider::Listener
{
    // ...

I thought code such as this: class MainContentComponent : public Component is used to tell c++ that MainContentComponent inherits from the Component class, giving it all of the features that Component has. Now it looks like it can inherit from multiple classes, and from within a class of a class that does not even belong to the same heritage of MainContentComponent?

As a side, I’m struggling quite a bit with just the sheer number of classes and managing the scope of every variable/function related to every class and function, is it public? is it private? and don’t even get me started on pointers!

Does anyone have any resources or tips they can point me to that will help me with this?

I can do pretty advanced things in Processing(.org) and I’m a master with PureData or Max/MSP, but getting to grips with C++ has always appeared to be beyond me.

0 Likes

#2

Have you take a look at tutorials ?
Like this one : https://docs.juce.com/master/tutorial_slider_values.html

0 Likes

#3

Yep! In C++ multiple inheritance is possible

It can get a pretty confusing IMO, mostly when it comes to virtual methods that might have vague names. Sometimes it can be hard to tell from a glance which class a virtual function or data member may have come from. You can usually achieve a similar kind of interface by composition, by using member objects instead of inheriting from the base classes.

In the case of the tutorial it would probably be trickier / more annoying than multiple inheritance as you’d have to create a custom Slider::Listener type and then add it as a member of MainContentComponent, and the listener would have to be passed references to any other members (or to the main component class itself) of MainContentComponent it needs access to… It all depends on what’s “visible” to the class, essentially.

1 Like

#4

Yes, multiple inheritance is a thing in C++. Juce dates back to a time when that likely seemed the best approach to implement things. It’s not necessarily the best approach these days, but they can’t easily change it all to a more modern system. However, they’ve partially done that, and for example Slider can now be used without inheriting from Slider::Listener. (The tutorials just don’t show you that new approach. For example, to set a callback to happen on the Slider’s value change you can assign a C++11 lambda into the Slider’s member onValueChange.)

0 Likes

#5

Just going to leave this here:

1 Like

#6

Great to see I’m not the only one to whom it’s proves challenging!

@nseaprotector, it’s litterally that tutorial that sparked this conversation! :slight_smile:

@Xenakios, happy to report that the lambda approach is discussed at the end of the Listeners and Broadcasters tutorial

Going back to the side point if I may, does anyone perhaps know of a good video tutorial that approaches the task of familiarising yourself with a new library/framework, or beginning your first big project etc.

One thing that would help, and I know it’s been discussed before in the forum, is an order to to the tutorials. Kind of like a zero to hero approach.

0 Likes

#7

Are you describing the case of composition? I decided to take a step back, and focus on solidifying my understanding of C++ and in the book(s) I’m working from, we finally got to multiple inheritance, then literally at the end there’s a note that says 'oh by the way, you can just add the object as a member object and that’ll achieve the same effect. Doing that is called Composition!"

…So I wanted to come back to this thread like Moses with his stone tablets!!

0 Likes