简体   繁体   中英

C++ casting a std::vector<Derived>* to std::vector<Base> …?

I'm making a vector of pointers to vectors:

    std::vector<std::vector<Base>*> all; // vector keeping pointers to vectors

and the problem is I have several vectors of derived classes:

    std::vector<Derived1> derived1; // vector of derived class
    std::vector<Derived2> derived2; // vector of derived class
    std::vector<Derived3> derived2; // vector of derived class

Is there any safe way to change the type of a pointer from vector<DerivedX> to vector<Base> ?

Every derived class shares certain behavior and they will be alive for most of the program's life.

I'm still not good at explaining my programming problems, should any questions concerning details arise - please ask in comments.

No, that is not possible. There is no relationship between std::vector<Base> and std::vector<Derived> .

In other words: std::vector<Derived> is NOT derived from std::vector<Base> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM