I’m creating a family of Component objects that I want to act polymorphicly, so I create a base class for them. The Base class itself is a Component, so that it can be added to Component hierarchy. This all compiles, but the derived Component never has its paint()
method called.
class MyBase : public Component
{};
class MySlider: public MyBase, public Slider
{};
class MainContentComponent : public Component
{
public:
std::unique_ptr<MyBase> slider;
MainContentComponent()
{
slider.reset(new MySlider());
addAndMakeVisible(*slider);
slider->setBounds(30, 30, 400, 100);
setBounds(0, 0, 600, 600);
}
};
// This code compiles, but the slider is never displayed.
How can I modify this code so that the paint()
method of the correct derived class is called?
The code I’ve posted probably looks a bit suspicious, so let me add a few comments:
-
MyBase
looks obsolete, but this is because I’ve omitted some details that are irrelevant to the question. In reality this class will have a lot of work to do, so I’d really like to hold on to it. -
I am aware of the ambiguity of
MySlider
inheriting fromComponent
twice. I was part expecting the code not to compile because of the diamond problem. It does compile, but I still suspect that the problem is due to this ambiguity.