Noob question for class inheritance!


#1

Hi everyone,

I have 3 classes each divided into cpp and header file.

1- MainContentComponent Class ( where components of A and B appear ).

2- Class A ( it has a slider component ).

3- Class B ( it has a label component ).

What I want to do is that I want the slider value to be shown in the label that is in a different class. I know how to make that possible inside a class but not in different classes.

The question is.... How should I inherit from class A in order to get the slider's value and show that in the class B where label is ?

I know that this might be easy but i'm new in juce and c++ too and the problem is that I just don't know how to inherit an object of another class in juce.

I tried by myself but failed because in the end i was able just to show all the components of class A in the class B and this was not what i wanted.

This is just a simple scenario because to be honest i have write alot of other components with multiple classes and when i tried to link their functions i stucked. :/

Anyway, forgive me if this is a noob question, but i want to get a solution for this as soon as possible. :)

 


#2

I don't think inheritance between your own classes is at all the proper thing to do for what you want to do...

One way to do what you want is to pass around pointers of the objects between the object instances as needed. For example pass the pointer of the label in class B into class A so that the slider in class A can change text in the label. (If the class A doesn't need full access to the class B, it's better to just pass the pointer of the label component.) How exactly to implement the passing of the pointers can be done in a few ways. The safest is probably to make the constructor of class A to require a pointer to a Label. 

Another way to achieve what you want would be to inherit from the Juce Listener classes as needed. For example the Slider has the Slider::Listener class. So you would inherit your class B from Slider::Listener etc.

 


#3

I don't think you need to be thinking about inheritance for what you want. You might just want to create public variables to communicate between the two classes. That seems like the easiest way. I'm new to C++ and JUCE too so I don't know if there's a better way,


#4

I can show you a simple idea how my codes might be so you can explain it to me a little more because i don't think i understood that well.

Here are the two classes :


#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
#include "../JuceLibraryCode/JuceHeader.h"
class ComponentA : public Component,
                   public Slider::Listener
{
public:
    ComponentA()
    :slider (Slider::LinearVertical, Slider::NoTextBox)
    {
        addAndMakeVisible(&slider);
        
        slider.setRange(0.0, 100.0, 1.0);
        slider.setValue(80.0);
        setSize(400, 120);
    }
    ~ComponentA(){}
    void paint(Graphics& g){
        g.fillAll(Colour(5, 5, 5));
    }
    void resized(){
        
        slider.setBounds(10, 10, 20, 80);
        
    }
    void sliderValueChanged(Slider* slid){
        if (&slider == slid)
        {
            label.setText(String(slider.getValue()), sendNotification);
        }
    }
    
        Slider slider;
private:
    
    
};
class ComponentB : public Component
{
public:
    ComponentB()
    
    {
        addAndMakeVisible(&label);
        
    
        setSize(400, 50);
    }
    ~ComponentB(){}
    void paint(Graphics& g){
        g.fillAll(Colour(5, 5, 5));
    }
    void resized(){
        
        label.setBounds(10, 10, 60, 30);
        
    }
    
        Label label;
private:
    
    
};


#endif  // TEST_H_INCLUDED

 


#5

It seems like the easiest way for me too, but it doesn't work.:/


#6

I would probably just create an object of ComponentB inside ComponentA and "communicate" between the two in ComponentA. Or maybe have a MainComponent and create objects of ComponentA and ComponentB and "communicate" in MainComponent. Like I said, I'm new too so I don't know the best way to work with Components yet. I hope you can find a way that works for you.