I have managed to fix, or rather work around, the issue, so I'm not looking for help but I'm just really curious what the underlying reason in the language is.
I have the following code in my (cocos2d-x, but I don't think that's relevant) game:
const Size& Checkbox::getContentSize() const
{
Size size;
size.height = label->getContentSize().width;
size.width = label->getContentSize().height + hPadding + checkbox->getContentSize().height;
return size;
}
The function returns a Size with only the width while the height remains 0. However, if I add any code before returning, the function returns a Size containing the correct width and height.
const Size& Checkbox::getContentSize() const
{
Size size;
size.height = label->getContentSize().width;
size.width = label->getContentSize().height + hPadding + checkbox->getContentSize().height;
int meaninglessInteger = 10;
return size; //size gets returned properly
}
It also returns properly if I do it like this:
const Size& Checkbox::getContentSize() const
{
float height = label->getContentSize().width;
float width = label->getContentSize().height + hPadding + checkbox->getContentSize().height;
return Size(width, height); //size gets returned properly
}
It's a virtual function from the cocos2d-x engine itself so I can't play around with the parameters or make anything not const or a reference. Anyway, I'd just like to know what causes this behavior, and especially why it gets fixed by adding code that does nothing.