Value::Listener for class derived from Label


#1

Apologies in advance for the potentially daft question, but I'm struggling to work out the best way to solve the following problem: 

I've created a class that derives from Label and has a member Value (relating to a plugin parameter). The idea is that if the Value is changed (e.g. when an slider/combobox that is attached to the same Value changes), the Label's text is updated accordingly*.

I can't make use of Label's inheritance of the Value::Listener, as that's private, hence getting the following error when trying to add my class as a listener to the particular Value:


Error    1    error C2243: 'type cast' : conversion from 'BCMLabel *const ' to 'juce::Value::Listener *' exists, but is inaccessible

I was able create a similar class deriving from ComboBox, but in that case, ComboBox inherits from ValueListener "public"ly. I can get the code to compile if I go with:

class BCMLabel : public Label, public ValueListener

However, I get compiler warnings and it doesn't really seem like the right thing to do... especially as it doesn't compile if I replace ValueListener with Value::Listener.

* I'm happy to explain why I want to be able to do this, but didn't want to bloat the question.


#2

Yes, it's tricky when a base class already derives from something that you want to use, but it's better for the Label to do so privately and cause a compile error than for it to be public and let you override it and accidentally break the functionality that the base class is using.

The normal way to do this is to create a quick inner class, e.g.

struct MyListener  : public Value::Listener

{

    ParentClass& parent;

    void valueChanged (Value& v)  { parent.handleMyValueChanged (v); }

};

Once everyone is using c++11 then I'll be able to use lambdas for stuff like this, which will mean less boilerplate like the class above, and less need for base class inheritance.

 


#3

Cool, I thought it would be something like that. I did try something like that earlier, but made a bit of a mess of it on the first go :)

Will have another try with your suggested approach tomorrow.