I have a component type class that has an object member which does some calculations. Now I want to call the repaint function of the outer class from within the object. How can I do that? Function pointers? Or is there an easier way?
to be precise. The object implements an osc receiver and I want to change an image of the parent component if there are osc pakets received in the object…
The usual “Juce way” would probably be to use ChangeBroadcaster and ChangeListener. (Inherit your OSC class also from ChangeBroadcaster and your GUI component from ChangeListener.)
A handy alternative way in C++11 and above could be to use a std::function member in your OSC class. (std::function can work like a function pointer, but it’s easier to use and also better than a function pointer because you can use lambdas with captures with it.)
Ok. I have now derived the outer class by the ChangeListener and the implemented inner object by the changeBoradcaster and I have implented the “virtual void changeListenerCallback(ChangeBroadcaster *source);” in the outer class.
Futhermore I have added a “ChangeListener *listener” to the inner’s object’s constructor parameter list and add it with “addChangeListener(listener);” in the inner’s object constructor.
But when I create my inner object, it now expects an pointer “ChangeListener *listener”. How do I get the listener pointer to pass it to the inner object?
You are correct in making your outer class inherit from ChangeListener and implement the changeListenerCallback method. However, you need to register your outer class (the ChangeListener) as a listener to your inner class (the ChangeBroadcaster) so that it can receive callbacks from your ChangeBroadcaster. This can be done by calling the addChangeListener function of your ChangeBroadcaster class from the ChangeListener class and passing it the this pointer as the argument. Then when you want to send a message to your ChangeListener class you can call the sendChangeMessage method in the ChangeBroadcaster.
However this is way overcomplicating the problem. Instead you can just pass a reference to your outer class to the inner class via its constructor and then when you want the outer class to repaint, just call the repaint function on this reference from the inner class.