Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 784
Usomochi Avatar asked Nov 22 '25 11:11

Usomochi


1 Answers

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>.

like image 60
D.R. Avatar answered Nov 25 '25 01:11

D.R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!