(C++) How to return a pointer to a class derived from Slider with Slider::Listener


#1

I'm pretty sure that I'm missing something fairly basic here, but I can't figure this out and it's driving me crazy:

How do I return a pointer to an instance of a class derived from Slider with Slider::Listener?

I have a class that inherits Slider, called DerivedSlider. In my main component, I inherit from Slider::Listener and override sliderValueChanged, which takes as argument a pointer to a Slider. I also create an instance of DerivedSlider, called derivedSlider. Here's what's confusing: When sliderValueChanged is called, I can compare the Slider pointer it receives as an argument to a reference to my derivedSlider, and it works just as I'd hope. However the Slider pointer then cannot be dereferenced to access members of the DerivedSlider object since it seems to point to the base part of my object.

How can it be that (slider == &derivedSlider) can be true, yet slider->derivedMethod() is illegal? Like I said, I think I'm missing something basic here but this is puzzling to me...

My first thought was to redefine the Listener class and related functions (addListener and removeListener) in my derived Class, but I can't seem to get it to work. I feel like I'm hitting a wall with the pimpl inside Slider. 

Thanks in advance. Any ideas?


#2

#3

It's because C++ is a statically typed language and you are comparing something that is checked at compile time to something that is checked at runtime.  Pretty much everything about a class and it's methods are checked at compile time.  The "==" works because it's just comparing two pointers to see if they point to the same memory.  It doesn't know or imply anything beyond that.  That said, if you have a reference to your derived slider, then just use that to call the methods it defines.  Once you know it's the slider that invoked the listener (from the "==" check), then you're good to go ahead and use derivedSlider.derivedMethod().

That said, if these sorts of things are confusing you it might be best to get a solid read on how C++ works.  This forum has some helpful folks but basic language stuff doesn't always get a response.  Just fyi.


#4

Thanks!

This is exactly what I was looking for. It's funny how often the more complicated approach seems so right at the time.

It seems like dynamic_cast is really only appropriate if I don't know what the type of object coming in is... simply using static_cast works for me in this case, since every slider object I'll be creating will be an instance of the DerivedSlider class. 


#5

Thanks Graeme,

Also very helpful. I'm admittedly quite new to C++ and it's often a bit difficult to search stackoverflow and such for answers for questions that I don't quite know how to ask outside of a JUCE context, such as this one. 

Really appreciate the answer though - I'm happy with how quickly I've come along with learning C++ through JUCE programming, and it's great to find that the forums have been very helpful thus far.


#6

Glad to hear it's going well. :)  I should probably clarify the "==" check so I don't give you the wrong impression.  The two pointers do have to have the same type (or base type) for it to pass compilation.  The compiler will give you an error if you, for example, try to compare a button pointer to a slider pointer.