mercredi 3 mai 2017

C++ std::array cast

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;

    virtual std::array<Foundation, 4>* get_foundations() {
        return &this->foundations;
    }
}

class GameWidget {
    std::array<FoundationWidget, 4> foundations;

    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?

Aucun commentaire:

Enregistrer un commentaire