Getting bounds in base class when using derived classes

Hi,

I’m having an issue getting the bounds of an instance in the base classes ‘resized()’ function. Easier to explain with pseudocode:

class Base : public Component
{
    void resized() override;
};

class Derived : public Base
{
    void resized() override;
};

void Base::resized()
{
    Rectangle<int> bounds = getBounds(); // width and height zero??
}

void Derived::resized()
{
    Base::resized();
    Rectangle<int> bounds = getBounds(); // width and height as expected
}

When calling Derived::resized() I can successfully get the components bounds but the Base class reports as width/height of 0?

if you don’t get this to work maybe you could alternatively override setBounds and call Base::setBounds(bounds) with it. not sure if that will call the Base resized with the proper bounds then, but maybe it does

The resized() is synchronously called when the bounds have changed.

I think your component has indeed the size of 0x0, because it was created after the parent Component already got a size set.

Note that if people say parent Component, they usually mean the one in the hierarchy of the Components containing sub components, and not inheritance.

And if Derived overrides the resized() callback, the Base classes resized() is no longer called. But for juce that is usually not necessary, because the resized() is a callback for you, the internal work is done in different places, that you usually don’t override.

1 Like

Hi Daniel, thanks for the response.

I’m not quite sure I understand though? As the Derived::resized() calls Base::resized(), in the Base::resized() the bounds are as expected (and non-zero) but in the Derived::resized() they are 0x0. But surely both Derived and Base are using the same Component instance?

Can you write a full example that reproduces that behaviour you’re seeing? The example you provided won’t compile as Base::resized() is private so Derived can’t call it. With a quick test, calling the base component’s resized method in the derived one’s produces the expected behavior where both have the same size.

This example works as expected for me:

class MainComponent  : public juce::Component
{
public:
    MainComponent()
    {
        addAndMakeVisible (derived);
        setSize (600, 400);
    }

    void resized() override
    {
        derived.setBounds(getLocalBounds());
    }

private:
    struct Base : public juce::Component
    {
        void resized() override
        {
            const auto bounds = getBounds();
            DBG ("Base's bounds: " << bounds.toString());
        }
    };

    struct Derived : public Base
    {
        void resized() override
        {
            Base::resized();

            const auto bounds = getBounds();
            DBG ("Derived's bounds: " << bounds.toString());
        }
    };

    Derived derived;
};

Outputs:

Base's bounds: 0 0 600 400
Derived's bounds: 0 0 600 400
2 Likes

Hi ImJimmi, thanks for your response.
In asking me to write a full repro case (which I probably should have done from the start!) I found the issue… In the Base class constructor, for some unknown reason, I was calling resized(). This meant that when the instance is first constructed the resized() method was calling getBounds() when the bounds were actually 0. This was never happening in the Derived::resized() as that was only being called when it should have been.

Apologies!