I have 4 classes: Foundation, FoundationWidget, Game and GameWidget. FoundationWidget extends Foundation and GameWidget extends Game.
Game also contains an std::array of Foundations and GameWidget contains an std::array of FoundationWidgets. Game also contains a virtual method, which should return the pointer to the array of Foundations. GameWidget overrides this method by returning a pointer to the array of its FoundationWidgets.
The current (non-working) implementation looks like this:
class Game {
std::array<Foundation, 4> foundations;
private:
virtual std::array<Foundation, 4>* get_foundations() {
return &this->foundations;
}
}
class GameWidget : public Game {
std::array<FoundationWidget, 4> foundations;
private:
std::array<Foundation, 4>* get_foundations() {
return &this->foundations;
}
}
I'd expect this to work, since it's an array of the same size with class, that extends the one specified as return type, but instead I'm getting this error: cannot convert ‘std::array<FoundationWidget, 4ul>*’ to ‘std::array<Foundation, 4ul>*’ in return.
I've also tried to declare the class attributes as arrays of pointers, but the result was the same. Neither static_cast or dynamic_cast helped.
What am I missing here? Is there any way to cast arrays? If not, can I use some construction to get the same results, i.e. "virtual" class members?
FoundationWidgetinherits fromFoundationwhy not just have astd::array<Foundation*, 4>(or some other pointer type inGameWidget?std::array<Foundation, 4>*which is a pointer type,if you are considering something like getter, you should be careful not return a temporary/local variable's pointer.Foo<Bar>is a complete different type fromFoo<Baz>even ifBazis derived fromBar. There is no conversion between them.